From 5b47b344b937611370f41d8f94a2489de7d71fa6 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Fri, 17 May 2024 21:10:55 +0530 Subject: [PATCH 1/5] feat: added shared metadata inside install table --- src/fpm/manifest/install.f90 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/fpm/manifest/install.f90 b/src/fpm/manifest/install.f90 index 88c3097eb0..bb6c53d0ec 100644 --- a/src/fpm/manifest/install.f90 +++ b/src/fpm/manifest/install.f90 @@ -19,6 +19,9 @@ module fpm_manifest_install !> Install library with this project logical :: library = .false. + !> Tells fpm to generate shared library. + logical :: shared = .false. + contains !> Print information on this instance @@ -51,6 +54,7 @@ subroutine new_install_config(self, table, error) if (allocated(error)) return call get_value(table, "library", self%library, .false.) + call get_value(table, "shared", self%shared, .false.) end subroutine new_install_config @@ -77,6 +81,8 @@ subroutine check(table, error) exit case("library") continue + case("shared") + continue end select end do if (allocated(error)) return @@ -109,6 +115,8 @@ subroutine info(self, unit, verbosity) write(unit, fmt) "Install configuration" write(unit, fmt) " - library install", & & trim(merge("enabled ", "disabled", self%library)) + write(unit, fmt) " - shared library", & + & trim(merge("enabled ", "disabled", self%shared)) end subroutine info @@ -118,9 +126,11 @@ logical function install_conf_same(this,that) install_conf_same = .false. + select type (other=>that) type is (install_config_t) if (this%library.neqv.other%library) return + if (this%shared.neqv.other%shared) return class default ! Not the same type return @@ -144,6 +154,7 @@ subroutine dump_to_toml(self, table, error) type(error_t), allocatable, intent(out) :: error call set_value(table, "library", self%library, error, class_name) + call set_value(table, "shared", self%shared, error, class_name) end subroutine dump_to_toml @@ -162,6 +173,7 @@ subroutine load_from_toml(self, table, error) integer :: stat call get_value(table, "library", self%library, error, class_name) + call get_value(table, "shared", self%shared, error, class_name) if (allocated(error)) return end subroutine load_from_toml From c819361df7f46e30d44cc962ce48b33cf94e49c0 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sun, 16 Jun 2024 13:06:59 +0530 Subject: [PATCH 2/5] fix: added shared library flag under build table --- src/fpm/manifest/build.f90 | 14 +++++++++++++- src/fpm/manifest/install.f90 | 11 ----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/fpm/manifest/build.f90 b/src/fpm/manifest/build.f90 index 035ea0d51d..0782f10bd7 100644 --- a/src/fpm/manifest/build.f90 +++ b/src/fpm/manifest/build.f90 @@ -41,6 +41,9 @@ module fpm_manifest_build !> External modules to use type(string_t), allocatable :: external_modules(:) + !> Generate a shared library. + logical :: shared_library = .false. + contains !> Print information on this instance @@ -135,6 +138,8 @@ subroutine new_build_config(self, table, package_name, error) call get_list(table, "external-modules", self%external_modules, error) if (allocated(error)) return + call get_value(table, "shared-library", self%shared_library, .false., stat=stat) + end subroutine new_build_config !> Check local schema for allowed entries @@ -160,7 +165,7 @@ subroutine check(table, package_name, error) do ikey = 1, size(list) select case(list(ikey)%key) - case("auto-executables", "auto-examples", "auto-tests", "link", "external-modules", "module-naming") + case("auto-executables", "auto-examples", "auto-tests", "link", "external-modules", "module-naming", "shared-library") continue case default @@ -216,6 +221,8 @@ subroutine info(self, unit, verbosity) end do end if + write(unit, fmt) " - generate shared library ", merge("enabled ", "disabled", self%shared_library) + end subroutine info !> Check that two dependency trees are equal @@ -235,6 +242,7 @@ logical function build_conf_is_same(this,that) if (.not.this%module_prefix==other%module_prefix) return if (.not.this%link==other%link) return if (.not.this%external_modules==other%external_modules) return + if (this%shared_library.neqv.other%shared_library) return class default ! Not the same type @@ -278,6 +286,8 @@ subroutine dump_to_toml(self, table, error) call set_list(table, "external-modules", self%external_modules, error) if (allocated(error)) return + call set_value(table, "shared-library", self%shared_library, error, class_name) + end subroutine dump_to_toml !> Read build config from toml table (no checks made at this stage) @@ -321,6 +331,8 @@ subroutine load_from_toml(self, table, error) call get_list(table, "external-modules", self%external_modules, error) if (allocated(error)) return + call get_value(table, "shared-library", self%shared_library, error, class_name) + end subroutine load_from_toml diff --git a/src/fpm/manifest/install.f90 b/src/fpm/manifest/install.f90 index bb6c53d0ec..e8cde34d00 100644 --- a/src/fpm/manifest/install.f90 +++ b/src/fpm/manifest/install.f90 @@ -19,9 +19,6 @@ module fpm_manifest_install !> Install library with this project logical :: library = .false. - !> Tells fpm to generate shared library. - logical :: shared = .false. - contains !> Print information on this instance @@ -54,7 +51,6 @@ subroutine new_install_config(self, table, error) if (allocated(error)) return call get_value(table, "library", self%library, .false.) - call get_value(table, "shared", self%shared, .false.) end subroutine new_install_config @@ -81,8 +77,6 @@ subroutine check(table, error) exit case("library") continue - case("shared") - continue end select end do if (allocated(error)) return @@ -115,8 +109,6 @@ subroutine info(self, unit, verbosity) write(unit, fmt) "Install configuration" write(unit, fmt) " - library install", & & trim(merge("enabled ", "disabled", self%library)) - write(unit, fmt) " - shared library", & - & trim(merge("enabled ", "disabled", self%shared)) end subroutine info @@ -130,7 +122,6 @@ logical function install_conf_same(this,that) select type (other=>that) type is (install_config_t) if (this%library.neqv.other%library) return - if (this%shared.neqv.other%shared) return class default ! Not the same type return @@ -154,7 +145,6 @@ subroutine dump_to_toml(self, table, error) type(error_t), allocatable, intent(out) :: error call set_value(table, "library", self%library, error, class_name) - call set_value(table, "shared", self%shared, error, class_name) end subroutine dump_to_toml @@ -173,7 +163,6 @@ subroutine load_from_toml(self, table, error) integer :: stat call get_value(table, "library", self%library, error, class_name) - call get_value(table, "shared", self%shared, error, class_name) if (allocated(error)) return end subroutine load_from_toml From fa2749b9b85091ea1da632e607f9a19ebc352053 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sun, 16 Jun 2024 13:10:46 +0530 Subject: [PATCH 3/5] feat: added logic for generating a shared library --- src/fpm.f90 | 13 +++++++++++-- src/fpm_compiler.F90 | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/fpm.f90 b/src/fpm.f90 index 37cf069c9c..c3b7a59826 100644 --- a/src/fpm.f90 +++ b/src/fpm.f90 @@ -12,12 +12,14 @@ module fpm use fpm_model, only: fpm_model_t, srcfile_t, show_model, fortran_features_t, & FPM_SCOPE_UNKNOWN, FPM_SCOPE_LIB, FPM_SCOPE_DEP, & FPM_SCOPE_APP, FPM_SCOPE_EXAMPLE, FPM_SCOPE_TEST -use fpm_compiler, only: new_compiler, new_archiver, set_cpp_preprocessor_flags +use fpm_compiler, only: new_compiler, new_archiver, set_cpp_preprocessor_flags, & + generate_shared_library use fpm_sources, only: add_executable_sources, add_sources_from_dir use fpm_targets, only: targets_from_sources, build_target_t, build_target_ptr, & - FPM_TARGET_EXECUTABLE, FPM_TARGET_ARCHIVE + FPM_TARGET_EXECUTABLE, FPM_TARGET_ARCHIVE, & + FPM_TARGET_OBJECT use fpm_manifest, only : get_package_data, package_config_t use fpm_meta, only : resolve_metapackages use fpm_error, only : error_t, fatal_error, fpm_stop @@ -467,6 +469,13 @@ subroutine cmd_build(settings) call build_package(targets,model,verbose=settings%verbose) endif +do i=1, size(targets) + if (targets(i)%ptr%target_type == FPM_TARGET_OBJECT .and. package%build%shared_library) then + !> Build directory for saving object files. + call model%compiler%generate_shared_library(model%package_name, targets(i)%ptr%output_dir) + exit + end if +enddo end subroutine cmd_build subroutine cmd_run(settings,test) diff --git a/src/fpm_compiler.F90 b/src/fpm_compiler.F90 index f173267659..03a5e8289b 100644 --- a/src/fpm_compiler.F90 +++ b/src/fpm_compiler.F90 @@ -46,6 +46,7 @@ module fpm_compiler implicit none public :: compiler_t, new_compiler, archiver_t, new_archiver, get_macros public :: debug +public :: generate_shared_library enum, bind(C) enumerator :: & @@ -114,6 +115,8 @@ module fpm_compiler procedure :: is_gnu !> Enumerate libraries, based on compiler and platform procedure :: enumerate_libraries + !> Function for generating shared library. + procedure :: generate_shared_library !> Serialization interface procedure :: serializable_is_same => compiler_is_same @@ -1104,6 +1107,18 @@ subroutine compile_fortran(self, input, output, args, log_file, stat) & echo=self%echo, verbose=self%verbose, redirect=log_file, exitstat=stat) end subroutine compile_fortran +subroutine generate_shared_library(self, package_name, output_dir) + !> Instance of the compiler object + class(compiler_t), intent(in) :: self + !> Name of the package. + character(len=*), intent(in) :: package_name + !> Output directory of object files. + character(len=*), intent(in) :: output_dir + + call run(self%fc // " --shared " // output_dir// "/" // package_name // "/*.o" // " -o " // "lib" // & + package_name // ".so", echo=self%echo, verbose=self%verbose) +end subroutine generate_shared_library + !> Compile a C object subroutine compile_c(self, input, output, args, log_file, stat) From 7aaa8a697fd0820f286d96debbdf56be8db6ef7a Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sun, 16 Jun 2024 16:58:58 +0530 Subject: [PATCH 4/5] fix: use archive instead of object files --- src/fpm.f90 | 2 +- src/fpm_compiler.F90 | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/fpm.f90 b/src/fpm.f90 index c3b7a59826..e264973639 100644 --- a/src/fpm.f90 +++ b/src/fpm.f90 @@ -470,7 +470,7 @@ subroutine cmd_build(settings) endif do i=1, size(targets) - if (targets(i)%ptr%target_type == FPM_TARGET_OBJECT .and. package%build%shared_library) then + if (targets(i)%ptr%target_type == FPM_TARGET_ARCHIVE .and. package%build%shared_library) then !> Build directory for saving object files. call model%compiler%generate_shared_library(model%package_name, targets(i)%ptr%output_dir) exit diff --git a/src/fpm_compiler.F90 b/src/fpm_compiler.F90 index 03a5e8289b..627491b07a 100644 --- a/src/fpm_compiler.F90 +++ b/src/fpm_compiler.F90 @@ -1115,8 +1115,9 @@ subroutine generate_shared_library(self, package_name, output_dir) !> Output directory of object files. character(len=*), intent(in) :: output_dir - call run(self%fc // " --shared " // output_dir// "/" // package_name // "/*.o" // " -o " // "lib" // & - package_name // ".so", echo=self%echo, verbose=self%verbose) + call run(self%fc // " --shared " // " -o " // "lib" // package_name // ".so" // " " & + // output_dir // "/" // package_name // "/" // "lib" // package_name & + // ".a", echo=self%echo, verbose=self%verbose) end subroutine generate_shared_library From 0c15986d82f3d29af6d515c40e18901d955e3c2c Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sun, 23 Jun 2024 19:44:58 +0530 Subject: [PATCH 5/5] refactor: directory use outfile of library archive --- src/fpm.f90 | 3 +-- src/fpm_compiler.F90 | 9 ++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/fpm.f90 b/src/fpm.f90 index e264973639..4a9d032472 100644 --- a/src/fpm.f90 +++ b/src/fpm.f90 @@ -471,8 +471,7 @@ subroutine cmd_build(settings) do i=1, size(targets) if (targets(i)%ptr%target_type == FPM_TARGET_ARCHIVE .and. package%build%shared_library) then - !> Build directory for saving object files. - call model%compiler%generate_shared_library(model%package_name, targets(i)%ptr%output_dir) + call model%compiler%generate_shared_library(model%package_name, targets(i)%ptr%output_file) exit end if enddo diff --git a/src/fpm_compiler.F90 b/src/fpm_compiler.F90 index 627491b07a..6ce32ffd24 100644 --- a/src/fpm_compiler.F90 +++ b/src/fpm_compiler.F90 @@ -1107,17 +1107,16 @@ subroutine compile_fortran(self, input, output, args, log_file, stat) & echo=self%echo, verbose=self%verbose, redirect=log_file, exitstat=stat) end subroutine compile_fortran -subroutine generate_shared_library(self, package_name, output_dir) +subroutine generate_shared_library(self, package_name, output_file) !> Instance of the compiler object class(compiler_t), intent(in) :: self !> Name of the package. character(len=*), intent(in) :: package_name - !> Output directory of object files. - character(len=*), intent(in) :: output_dir + !> Output file of library archive. + character(len=*), intent(in) :: output_file call run(self%fc // " --shared " // " -o " // "lib" // package_name // ".so" // " " & - // output_dir // "/" // package_name // "/" // "lib" // package_name & - // ".a", echo=self%echo, verbose=self%verbose) + // output_file, echo=self%echo, verbose=self%verbose) end subroutine generate_shared_library