From 2fa920c0cd5c186d0991c9636c9af6691ead8dce Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 16 Jul 2019 10:32:25 -0400 Subject: AIX: Create import library for executables with exports On AIX, plugins meant to be loaded into executables via `dlopen` must be linked with access to a list of symbols exported from the executable in order to use them (when not using runtime linking). The AIX linker supports specifying this list as an "import file" passed on the command line either via the `-bI:...` option or (with a leading `#! .` line) as a normal input file like any other library file. The linker import file plays the same role on AIX as import libraries do on Windows. Teach CMake to enable its import library abstraction on AIX for executables with the `ENABLE_EXPORTS` target property set. Teach our internal `ExportImportList` script to optionally generate a leading `#! .` line at the top of the generated export/import list. Update our rule for linking an executable with exports to generate a public-facing "import library" implemented as an AIX linker import file. With this approach, our existing infrastructure for handling import libraries on Windows will now work for AIX linker import files too: * Plugins that link to their executable's symbols will be automatically linked using the import file on the command line. * The executable's import file will be (optionally) installed and exported for use in linking externally-built plugins. This will allow executables and their plugins to build even if we later turn off runtime linking. Issue: #19163 --- Help/command/install.rst | 2 ++ Help/manual/cmake-buildsystem.7.rst | 4 ++++ Help/prop_tgt/ENABLE_EXPORTS.rst | 4 ++++ Help/prop_tgt/IMPORTED_IMPLIB.rst | 6 ++++-- Help/release/dev/aix.rst | 10 ++++++++++ Modules/Platform/AIX-GNU.cmake | 4 ++-- Modules/Platform/AIX-XL.cmake | 4 ++-- Modules/Platform/AIX.cmake | 2 ++ Modules/Platform/AIX/ExportImportList | 7 ++++++- Source/cmComputeLinkInformation.cxx | 9 +++++++++ Source/cmGeneratorTarget.cxx | 3 ++- Source/cmInstallCommand.cxx | 3 ++- Source/cmTarget.cxx | 9 ++++++--- Tests/RunCMake/FileAPI/codemodel-v2-check.py | 9 +++++++++ .../TARGET_FILE_PREFIX-imported-target.cmake | 2 +- Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX.cmake | 2 +- .../TARGET_FILE_SUFFIX-imported-target.cmake | 2 +- Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX.cmake | 2 +- 18 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 Help/release/dev/aix.rst diff --git a/Help/command/install.rst b/Help/command/install.rst index ab6fef6..5affc5b 100644 --- a/Help/command/install.rst +++ b/Help/command/install.rst @@ -126,6 +126,8 @@ project. There are several kinds of target files that may be installed: marked with the ``FRAMEWORK`` property on macOS (see ``FRAMEWORK`` below.) For DLL platforms (all Windows-based systems including Cygwin), the DLL import library is treated as an ``ARCHIVE`` target. + On AIX, the linker import file created for executables with + :prop_tgt:`ENABLE_EXPORTS` is treated as an ``ARCHIVE`` target. ``LIBRARY`` Module libraries are always treated as ``LIBRARY`` targets. For non- diff --git a/Help/manual/cmake-buildsystem.7.rst b/Help/manual/cmake-buildsystem.7.rst index 8cd6e68..d8142a2 100644 --- a/Help/manual/cmake-buildsystem.7.rst +++ b/Help/manual/cmake-buildsystem.7.rst @@ -811,6 +811,10 @@ An *archive* output artifact of a buildsystem target may be: executable target created by the :command:`add_executable` command when its :prop_tgt:`ENABLE_EXPORTS` target property is set. +* On AIX: the linker import file (e.g. ``.imp``) of an executable target + created by the :command:`add_executable` command when its + :prop_tgt:`ENABLE_EXPORTS` target property is set. + The :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY` and :prop_tgt:`ARCHIVE_OUTPUT_NAME` target properties may be used to control archive output artifact locations and names in the build tree. diff --git a/Help/prop_tgt/ENABLE_EXPORTS.rst b/Help/prop_tgt/ENABLE_EXPORTS.rst index daef78b..0b1064a 100644 --- a/Help/prop_tgt/ENABLE_EXPORTS.rst +++ b/Help/prop_tgt/ENABLE_EXPORTS.rst @@ -19,6 +19,10 @@ varies by platform: * On macOS, loadable modules link to the executable itself using the ``-bundle_loader`` flag. +* On AIX, a linker "import file" is created along with the executable + to list the exported symbols for import when linking other targets. + Loadable modules link to the import file to get the symbols. + * On other platforms, loadable modules are simply linked without referencing the executable since the dynamic loader will automatically bind symbols when the module is loaded. diff --git a/Help/prop_tgt/IMPORTED_IMPLIB.rst b/Help/prop_tgt/IMPORTED_IMPLIB.rst index 77fb552..c8b6fde 100644 --- a/Help/prop_tgt/IMPORTED_IMPLIB.rst +++ b/Help/prop_tgt/IMPORTED_IMPLIB.rst @@ -3,5 +3,7 @@ IMPORTED_IMPLIB Full path to the import library for an ``IMPORTED`` target. -Set this to the location of the ``.lib`` part of a Windows DLL. Ignored -for non-imported targets. +Set this to the location of the ``.lib`` part of a Windows DLL, or on +AIX set it to an import file created for executables that export symbols +(see the :prop_tgt:`ENABLE_EXPORTS` target property). +Ignored for non-imported targets. diff --git a/Help/release/dev/aix.rst b/Help/release/dev/aix.rst new file mode 100644 index 0000000..c24872e --- /dev/null +++ b/Help/release/dev/aix.rst @@ -0,0 +1,10 @@ +aix +--- + +* On AIX, executables using the :prop_tgt:`ENABLE_EXPORTS` target property + now produce a linker import file with a ``.imp`` extension in addition + to the executable file. Plugins (created via :command:`add_library` with + the ``MODULE`` option) that use :command:`target_link_libraries` to link + to the executable for its symbols are now linked using the import file. + The :command:`install(TARGETS)` command now installs the import file as + an ``ARCHIVE`` artifact. diff --git a/Modules/Platform/AIX-GNU.cmake b/Modules/Platform/AIX-GNU.cmake index cec71a1..9e981c6 100644 --- a/Modules/Platform/AIX-GNU.cmake +++ b/Modules/Platform/AIX-GNU.cmake @@ -34,6 +34,6 @@ macro(__aix_compiler_gnu lang) ) set(CMAKE_${lang}_LINK_EXECUTABLE_WITH_EXPORTS - "\"${CMAKE_ROOT}/Modules/Platform/AIX/ExportImportList\" -o /objects.exp " - " -Wl,-bE:/objects.exp -o ") + "\"${CMAKE_ROOT}/Modules/Platform/AIX/ExportImportList\" -o -l . " + " -Wl,-bE: -o ") endmacro() diff --git a/Modules/Platform/AIX-XL.cmake b/Modules/Platform/AIX-XL.cmake index 52292bd..3424e2c 100644 --- a/Modules/Platform/AIX-XL.cmake +++ b/Modules/Platform/AIX-XL.cmake @@ -32,6 +32,6 @@ macro(__aix_compiler_xl lang) ) set(CMAKE_${lang}_LINK_EXECUTABLE_WITH_EXPORTS - "\"${CMAKE_ROOT}/Modules/Platform/AIX/ExportImportList\" -o /objects.exp " - " -Wl,-bE:/objects.exp -o ") + "\"${CMAKE_ROOT}/Modules/Platform/AIX/ExportImportList\" -o -l . " + " -Wl,-bE: -o ") endmacro() diff --git a/Modules/Platform/AIX.cmake b/Modules/Platform/AIX.cmake index 551a995..03cef51 100644 --- a/Modules/Platform/AIX.cmake +++ b/Modules/Platform/AIX.cmake @@ -1,5 +1,7 @@ set(CMAKE_SHARED_LIBRARY_PREFIX "lib") # lib set(CMAKE_SHARED_LIBRARY_SUFFIX ".so") # .so +set(CMAKE_AIX_IMPORT_FILE_PREFIX "") +set(CMAKE_AIX_IMPORT_FILE_SUFFIX ".imp") set(CMAKE_DL_LIBS "-lld") # RPATH support on AIX is called libpath. By default the runtime diff --git a/Modules/Platform/AIX/ExportImportList b/Modules/Platform/AIX/ExportImportList index c17378c..4f67ef5 100755 --- a/Modules/Platform/AIX/ExportImportList +++ b/Modules/Platform/AIX/ExportImportList @@ -5,7 +5,7 @@ # This script is internal to CMake and meant only to be # invoked by CMake-generated build systems on AIX. -usage='usage: ExportImportList -o [--] ...' +usage='usage: ExportImportList -o [-l ] [--] ...' die() { echo "$@" 1>&2; exit 1 @@ -13,8 +13,10 @@ die() { # Process command-line arguments. out='' +lib='' while test "$#" != 0; do case "$1" in + -l) shift; lib="$1" ;; -o) shift; out="$1" ;; --) shift; break ;; -*) die "$usage" ;; @@ -44,5 +46,8 @@ done > "$out_tmp" # Generate the export/import file. { + if test -n "$lib"; then + echo "#! $lib" + fi sort -u "$out_tmp" } > "$out" diff --git a/Source/cmComputeLinkInformation.cxx b/Source/cmComputeLinkInformation.cxx index 5a03670..f696f28 100644 --- a/Source/cmComputeLinkInformation.cxx +++ b/Source/cmComputeLinkInformation.cxx @@ -616,6 +616,15 @@ void cmComputeLinkInformation::AddItem(std::string const& item, // Pass the full path to the target file. std::string lib = tgt->GetFullPath(config, artifact, true); + if (tgt->Target->IsAIX() && cmHasLiteralSuffix(lib, "-NOTFOUND") && + artifact == cmStateEnums::ImportLibraryArtifact) { + // This is an imported executable on AIX that has ENABLE_EXPORTS + // but not IMPORTED_IMPLIB. CMake used to produce and accept such + // imported executables on AIX before we taught it to use linker + // import files. For compatibility, simply skip linking to this + // executable as we did before. It works with runtime linking. + return; + } if (!this->LinkDependsNoShared || tgt->GetType() != cmStateEnums::SHARED_LIBRARY) { this->Depends.push_back(lib); diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index a2c0503..d3e8248 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -6363,7 +6363,8 @@ bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const this->IsExecutableWithExports()) && // Assemblies which have only managed code do not have // import libraries. - this->GetManagedType(config) != ManagedType::Managed); + this->GetManagedType(config) != ManagedType::Managed) || + (this->Target->IsAIX() && this->IsExecutableWithExports()); } bool cmGeneratorTarget::NeedImportLibraryName(std::string const& config) const diff --git a/Source/cmInstallCommand.cxx b/Source/cmInstallCommand.cxx index 750ed8c..3b0659c 100644 --- a/Source/cmInstallCommand.cxx +++ b/Source/cmInstallCommand.cxx @@ -637,7 +637,8 @@ bool cmInstallCommand::HandleTargetsMode(std::vector const& args) // On DLL platforms an executable may also have an import // library. Install it to the archive destination if it // exists. - if (target.IsDLLPlatform() && !archiveArgs.GetDestination().empty() && + if ((target.IsDLLPlatform() || target.IsAIX()) && + !archiveArgs.GetDestination().empty() && target.IsExecutableWithExports()) { // The import library uses the ARCHIVE properties. archiveGenerator = CreateInstallTargetGenerator( diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 9290d27..b1a0127 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -1716,7 +1716,8 @@ const char* cmTarget::GetSuffixVariableInternal( ? "CMAKE_SHARED_LIBRARY_SUFFIX" : "CMAKE_EXECUTABLE_SUFFIX"); case cmStateEnums::ImportLibraryArtifact: - return "CMAKE_IMPORT_LIBRARY_SUFFIX"; + return (impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_SUFFIX" + : "CMAKE_IMPORT_LIBRARY_SUFFIX"); } break; default: @@ -1756,7 +1757,8 @@ const char* cmTarget::GetPrefixVariableInternal( ? "CMAKE_SHARED_LIBRARY_PREFIX" : ""); case cmStateEnums::ImportLibraryArtifact: - return "CMAKE_IMPORT_LIBRARY_PREFIX"; + return (impl->IsAIX ? "CMAKE_AIX_IMPORT_FILE_PREFIX" + : "CMAKE_IMPORT_LIBRARY_PREFIX"); } break; default: @@ -1892,7 +1894,8 @@ bool cmTarget::GetMappedConfig(std::string const& desired_config, // library or an executable with exports. bool allowImp = (this->IsDLLPlatform() && (this->GetType() == cmStateEnums::SHARED_LIBRARY || - this->IsExecutableWithExports())); + this->IsExecutableWithExports())) || + (this->IsAIX() && this->IsExecutableWithExports()); // If a mapping was found, check its configurations. for (std::vector::const_iterator mci = mappedConfigs.begin(); diff --git a/Tests/RunCMake/FileAPI/codemodel-v2-check.py b/Tests/RunCMake/FileAPI/codemodel-v2-check.py index 18b9347..3b0ec6e 100644 --- a/Tests/RunCMake/FileAPI/codemodel-v2-check.py +++ b/Tests/RunCMake/FileAPI/codemodel-v2-check.py @@ -979,6 +979,11 @@ def gen_check_targets(c, g, inSource): "_dllExtra": False, }, { + "path": "^lib/my_interface_exe\\.imp$", + "_aixExtra": True, + "_dllExtra": False, + }, + { "path": "^lib/((Debug|Release|RelWithDebInfo|MinSizeRel)/)?(lib)?my_interface_exe\\.(dll\\.a|lib)$", "_dllExtra": True, }, @@ -4895,6 +4900,10 @@ def gen_check_targets(c, g, inSource): for e in expected: e["artifacts"] = filter_list(lambda a: not a["_dllExtra"], e["artifacts"]) + if "aix" not in sys.platform: + for e in expected: + e["artifacts"] = filter_list(lambda a: not a.get("_aixExtra", False), e["artifacts"]) + return expected def check_targets(c, g, inSource): diff --git a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX-imported-target.cmake b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX-imported-target.cmake index 34e500a..f52776e 100644 --- a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX-imported-target.cmake +++ b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX-imported-target.cmake @@ -38,7 +38,7 @@ set_property (TARGET static2 PROPERTY IMPORT_PREFIX static2_import_prefix) string (APPEND GENERATE_CONTENT "\ncheck_value (\"TARGET_FILE_PREFIX executable custom\" \"$\" \"exec2_prefix\") -check_value (\"TARGET_LINKER_FILE_PREFIX executable linker custom\" \"$\" \"$,${win_platforms}>,exec2_import_prefix,exec2_prefix>\") +check_value (\"TARGET_LINKER_FILE_PREFIX executable linker custom\" \"$\" \"$,${win_platforms};AIX>,exec2_import_prefix,exec2_prefix>\") check_value (\"TARGET_FILE_PREFIX shared custom\" \"$\" \"shared2_prefix\") check_value (\"TARGET_LINKER_FILE_PREFIX shared linker custom\" \"$\" \"$,${win_platforms}>,shared2_import_prefix,shared2_prefix>\") check_value (\"TARGET_FILE_PREFIX static custom\" \"$\" \"static2_prefix\") diff --git a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX.cmake b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX.cmake index 6bb1e44..bef7bbf 100644 --- a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX.cmake +++ b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_PREFIX.cmake @@ -38,7 +38,7 @@ set_property (TARGET static2 PROPERTY IMPORT_PREFIX static2_import_prefix) string (APPEND GENERATE_CONTENT "\ncheck_value (\"TARGET_FILE_PREFIX executable custom\" \"$\" \"exec2_prefix\") -check_value (\"TARGET_LINKER_FILE_PREFIX executable linker custom\" \"$\" \"$,${win_platforms}>,exec2_import_prefix,exec2_prefix>\") +check_value (\"TARGET_LINKER_FILE_PREFIX executable linker custom\" \"$\" \"$,${win_platforms};AIX>,exec2_import_prefix,exec2_prefix>\") check_value (\"TARGET_FILE_PREFIX shared custom\" \"$\" \"shared2_prefix\") check_value (\"TARGET_LINKER_FILE_PREFIX shared linker custom\" \"$\" \"$,${win_platforms}>,shared2_import_prefix,shared2_prefix>\") check_value (\"TARGET_FILE_PREFIX static custom\" \"$\" \"static2_prefix\") diff --git a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX-imported-target.cmake b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX-imported-target.cmake index e1b7654..cefeb86 100644 --- a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX-imported-target.cmake +++ b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX-imported-target.cmake @@ -38,7 +38,7 @@ set_property (TARGET static2 PROPERTY IMPORT_SUFFIX static2_import_suffix) string (APPEND GENERATE_CONTENT "\ncheck_value (\"TARGET_FILE_SUFFIX executable custom\" \"$\" \"exec2_suffix\") -check_value (\"TARGET_LINKER_FILE_SUFFIX executable linker custom\" \"$\" \"$,${win_platforms}>,exec2_import_suffix,exec2_suffix>\") +check_value (\"TARGET_LINKER_FILE_SUFFIX executable linker custom\" \"$\" \"$,${win_platforms};AIX>,exec2_import_suffix,exec2_suffix>\") check_value (\"TARGET_FILE_SUFFIX shared custom\" \"$\" \"shared2_suffix\") check_value (\"TARGET_LINKER_FILE_SUFFIX shared linker custom\" \"$\" \"$,${win_platforms}>,shared2_import_suffix,shared2_suffix>\") check_value (\"TARGET_FILE_SUFFIX static custom\" \"$\" \"static2_suffix\") diff --git a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX.cmake b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX.cmake index 78afecd..39e39fd 100644 --- a/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX.cmake +++ b/Tests/RunCMake/GeneratorExpression/TARGET_FILE_SUFFIX.cmake @@ -38,7 +38,7 @@ set_property (TARGET static2 PROPERTY IMPORT_SUFFIX static2_import_suffix) string (APPEND GENERATE_CONTENT "\ncheck_value (\"TARGET_FILE_SUFFIX executable custom\" \"$\" \"exec2_suffix\") -check_value (\"TARGET_LINKER_FILE_SUFFIX executable linker custom\" \"$\" \"$,${win_platforms}>,exec2_import_suffix,exec2_suffix>\") +check_value (\"TARGET_LINKER_FILE_SUFFIX executable linker custom\" \"$\" \"$,${win_platforms};AIX>,exec2_import_suffix,exec2_suffix>\") check_value (\"TARGET_FILE_SUFFIX shared custom\" \"$\" \"shared2_suffix\") check_value (\"TARGET_LINKER_FILE_SUFFIX shared linker custom\" \"$\" \"$,${win_platforms}>,shared2_import_suffix,shared2_suffix>\") check_value (\"TARGET_FILE_SUFFIX static custom\" \"$\" \"static2_suffix\") -- cgit v0.12