From ce078dda79df1c8d9f142e45d2fa3cf971a09594 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 3 Apr 2019 14:42:35 -0400 Subject: Relax the usage of TARGET_OBJECTS generator expression The geneator expression can now be used with static, shared, and module libraries and executables. --- Help/manual/cmake-generator-expressions.7.rst | 3 +- .../relax-TARGET_OBJECTS-generator-expression.rst | 5 +++ Source/cmGeneratorExpressionNode.cxx | 10 ++++- Tests/GeneratorExpression/CMakeLists.txt | 45 ++++++++++++++++++++++ Tests/ObjectLibrary/CMakeLists.txt | 10 +++++ .../OutputNameMatchesObjects-stderr.txt | 3 +- .../File_Generate/OutputNameMatchesObjects.cmake | 7 +--- .../ObjectLibrary/BadSourceExpression3-stderr.txt | 5 ++- .../ObjectLibrary/BadSourceExpression3.cmake | 2 +- .../ObjectLibrary/CheckTargetObjects.cmake | 32 +++++++++++++++ Tests/RunCMake/ObjectLibrary/RunCMakeTest.cmake | 5 +++ .../ObjectLibrary/check_object_files.cmake | 17 ++++++++ .../TargetObjects/NotObjlibTarget-stderr.txt | 9 +++-- Tests/RunCMake/TargetObjects/NotObjlibTarget.cmake | 4 +- 14 files changed, 138 insertions(+), 19 deletions(-) create mode 100644 Help/release/dev/relax-TARGET_OBJECTS-generator-expression.rst create mode 100644 Tests/RunCMake/ObjectLibrary/CheckTargetObjects.cmake create mode 100644 Tests/RunCMake/ObjectLibrary/check_object_files.cmake diff --git a/Help/manual/cmake-generator-expressions.7.rst b/Help/manual/cmake-generator-expressions.7.rst index 8cb0fe7..d1f779b 100644 --- a/Help/manual/cmake-generator-expressions.7.rst +++ b/Help/manual/cmake-generator-expressions.7.rst @@ -461,8 +461,7 @@ Output-Related Expressions Content of ``...`` converted to a C identifier. The conversion follows the same behavior as :command:`string(MAKE_C_IDENTIFIER)`. ``$`` - List of objects resulting from build of ``objLib``. ``objLib`` must be an - object of type ``OBJECT_LIBRARY``. + List of objects resulting from build of ``objLib``. ``$`` Content of ``...`` converted to shell path style. For example, slashes are converted to backslashes in Windows shells and drive letters are converted diff --git a/Help/release/dev/relax-TARGET_OBJECTS-generator-expression.rst b/Help/release/dev/relax-TARGET_OBJECTS-generator-expression.rst new file mode 100644 index 0000000..25ca0c9 --- /dev/null +++ b/Help/release/dev/relax-TARGET_OBJECTS-generator-expression.rst @@ -0,0 +1,5 @@ +relax-TARGET_OBJECTS-generator-expression +----------------------------------------- + +* The ``TARGET_OBJECTS`` :manual:`generator expression ` + is now supported on ``SHARED``, ``STATIC``, ``MODULE`` libraries and executables. diff --git a/Source/cmGeneratorExpressionNode.cxx b/Source/cmGeneratorExpressionNode.cxx index 50413c8..7cef1c3 100644 --- a/Source/cmGeneratorExpressionNode.cxx +++ b/Source/cmGeneratorExpressionNode.cxx @@ -1525,10 +1525,16 @@ static const struct TargetObjectsNode : public cmGeneratorExpressionNode reportError(context, content->GetOriginalExpression(), e.str()); return std::string(); } - if (gt->GetType() != cmStateEnums::OBJECT_LIBRARY) { + cmStateEnums::TargetType type = gt->GetType(); + if (type != cmStateEnums::EXECUTABLE && + type != cmStateEnums::STATIC_LIBRARY && + type != cmStateEnums::SHARED_LIBRARY && + type != cmStateEnums::MODULE_LIBRARY && + type != cmStateEnums::OBJECT_LIBRARY) { std::ostringstream e; e << "Objects of target \"" << tgtName - << "\" referenced but is not an OBJECT library."; + << "\" referenced but is not an allowed library types (EXECUTABLE, " + << "STATIC, SHARED, MODULE, OBJECT)."; reportError(context, content->GetOriginalExpression(), e.str()); return std::string(); } diff --git a/Tests/GeneratorExpression/CMakeLists.txt b/Tests/GeneratorExpression/CMakeLists.txt index df0c78d..5ba0dc0 100644 --- a/Tests/GeneratorExpression/CMakeLists.txt +++ b/Tests/GeneratorExpression/CMakeLists.txt @@ -374,4 +374,49 @@ if(NOT CMAKE_GENERATOR STREQUAL Xcode OR NOT CMAKE_OSX_ARCHITECTURES MATCHES "[; -P "${CMAKE_CURRENT_SOURCE_DIR}/check_object_files.cmake" DEPENDS objlib ) + + + add_library(sharedlib SHARED objlib1.c objlib2.c) + file(GENERATE + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/sharedlib_files_$" + CONTENT "$,\n>\n" + ) + + add_custom_target(check_sharedlib_objs ALL + COMMAND ${CMAKE_COMMAND} + "-DOBJLIB_LISTFILE=${CMAKE_CURRENT_BINARY_DIR}/sharedlib_files_$" + -DEXPECTED_NUM_OBJECTFILES=2 + -P "${CMAKE_CURRENT_SOURCE_DIR}/check_object_files.cmake" + DEPENDS sharedlib + ) + + + add_library(staticlib STATIC objlib1.c objlib2.c) + file(GENERATE + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/staticlib_files_$" + CONTENT "$,\n>\n" + ) + + add_custom_target(check_staticlib_objs ALL + COMMAND ${CMAKE_COMMAND} + "-DOBJLIB_LISTFILE=${CMAKE_CURRENT_BINARY_DIR}/staticlib_files_$" + -DEXPECTED_NUM_OBJECTFILES=2 + -P "${CMAKE_CURRENT_SOURCE_DIR}/check_object_files.cmake" + DEPENDS staticlib + ) + + + add_executable(execobjs objlib1.c objlib2.c echo.c) + file(GENERATE + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/execobjs_files_$" + CONTENT "$,\n>\n" + ) + + add_custom_target(check_exec_objs ALL + COMMAND ${CMAKE_COMMAND} + "-DOBJLIB_LISTFILE=${CMAKE_CURRENT_BINARY_DIR}/execobjs_files_$" + -DEXPECTED_NUM_OBJECTFILES=3 + -P "${CMAKE_CURRENT_SOURCE_DIR}/check_object_files.cmake" + DEPENDS execobjs + ) endif() diff --git a/Tests/ObjectLibrary/CMakeLists.txt b/Tests/ObjectLibrary/CMakeLists.txt index 4bffd12..7897ab9 100644 --- a/Tests/ObjectLibrary/CMakeLists.txt +++ b/Tests/ObjectLibrary/CMakeLists.txt @@ -62,4 +62,14 @@ add_custom_target(UseABinternalDep COMMAND ${CMAKE_COMMAND} -E touch UseABintern add_custom_command(TARGET UseABinternal POST_BUILD COMMAND ${CMAKE_COMMAND} -P UseABinternalDep.cmake) add_dependencies(UseABinternal UseABinternalDep) +# Test a static library with sources from a different static library +add_library(UseCstaticObjs STATIC $ $ $) + +# Test a shared library with sources from a different shared library +add_library(UseCsharedObjs SHARED $ $ $) + +# Test a shared executable with sources from a different shared library +add_executable(UseABstaticObjs $) +target_link_libraries(UseABstaticObjs ABstatic) + add_subdirectory(ExportLanguages) diff --git a/Tests/RunCMake/File_Generate/OutputNameMatchesObjects-stderr.txt b/Tests/RunCMake/File_Generate/OutputNameMatchesObjects-stderr.txt index b08ef5a..4c2e35f 100644 --- a/Tests/RunCMake/File_Generate/OutputNameMatchesObjects-stderr.txt +++ b/Tests/RunCMake/File_Generate/OutputNameMatchesObjects-stderr.txt @@ -3,6 +3,7 @@ CMake Error at OutputNameMatchesObjects.cmake:[0-9]+ \(file\): \$ - Objects of target "foo" referenced but is not an OBJECT library. + Objects of target "foo" referenced but is not an allowed library types + \(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT\). Call Stack \(most recent call first\): CMakeLists.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/File_Generate/OutputNameMatchesObjects.cmake b/Tests/RunCMake/File_Generate/OutputNameMatchesObjects.cmake index daa7c49..84825fe 100644 --- a/Tests/RunCMake/File_Generate/OutputNameMatchesObjects.cmake +++ b/Tests/RunCMake/File_Generate/OutputNameMatchesObjects.cmake @@ -1,11 +1,8 @@ enable_language(CXX) file(GENERATE - OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$>somefile.cpp" + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$/somefile.cpp" CONTENT "static const char content[] = \"$\";\n" ) -add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/input.txt" - COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/input.txt" "${CMAKE_CURRENT_BINARY_DIR}") - -add_executable(foo empty.cpp "${CMAKE_CURRENT_BINARY_DIR}/1somefile.cpp" "${CMAKE_CURRENT_BINARY_DIR}/input.txt") +add_library(foo INTERFACE ) diff --git a/Tests/RunCMake/ObjectLibrary/BadSourceExpression3-stderr.txt b/Tests/RunCMake/ObjectLibrary/BadSourceExpression3-stderr.txt index 838b3d8..4dbd861 100644 --- a/Tests/RunCMake/ObjectLibrary/BadSourceExpression3-stderr.txt +++ b/Tests/RunCMake/ObjectLibrary/BadSourceExpression3-stderr.txt @@ -3,6 +3,7 @@ CMake Error at BadSourceExpression3.cmake:2 \(add_library\): \$ - Objects of target "NotObjLib" referenced but is not an OBJECT library. + Objects of target "NotObjLib" referenced but is not an allowed library + types \(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT\). Call Stack \(most recent call first\): - CMakeLists.txt:3 \(include\) + CMakeLists.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/ObjectLibrary/BadSourceExpression3.cmake b/Tests/RunCMake/ObjectLibrary/BadSourceExpression3.cmake index c3d9a62..4e07ea6 100644 --- a/Tests/RunCMake/ObjectLibrary/BadSourceExpression3.cmake +++ b/Tests/RunCMake/ObjectLibrary/BadSourceExpression3.cmake @@ -1,2 +1,2 @@ -add_library(NotObjLib STATIC a.c) +add_library(NotObjLib INTERFACE) add_library(A STATIC a.c $) diff --git a/Tests/RunCMake/ObjectLibrary/CheckTargetObjects.cmake b/Tests/RunCMake/ObjectLibrary/CheckTargetObjects.cmake new file mode 100644 index 0000000..0c85c72 --- /dev/null +++ b/Tests/RunCMake/ObjectLibrary/CheckTargetObjects.cmake @@ -0,0 +1,32 @@ +add_library(StaticLib STATIC a.c) + +add_custom_command(TARGET StaticLib POST_BUILD + VERBATIM + COMMAND ${CMAKE_COMMAND} + "-DTARGET_OBJECTS=$" + -DEXPECTED_NUM_OBJECTFILES=2 + -P "${CMAKE_CURRENT_SOURCE_DIR}/check_object_files.cmake" + ) + +add_library(SharedLib SHARED a.c b.c) +target_compile_definitions(SharedLib PRIVATE REQUIRED) + +add_custom_command(TARGET SharedLib POST_BUILD + VERBATIM + COMMAND ${CMAKE_COMMAND} + "-DTARGET_OBJECTS:STRING=$" + -DEXPECTED_NUM_OBJECTFILES=2 + -P "${CMAKE_CURRENT_SOURCE_DIR}/check_object_files.cmake" + ) + +add_executable(ExecObjs a.c b.c exe.c) +target_compile_definitions(ExecObjs PRIVATE REQUIRED) + +add_custom_target(check_exec_objs ALL + VERBATIM + COMMAND ${CMAKE_COMMAND} + "-DTARGET_OBJECTS=$" + -DEXPECTED_NUM_OBJECTFILES=3 + -P "${CMAKE_CURRENT_SOURCE_DIR}/check_object_files.cmake" + DEPENDS ExecObjs + ) diff --git a/Tests/RunCMake/ObjectLibrary/RunCMakeTest.cmake b/Tests/RunCMake/ObjectLibrary/RunCMakeTest.cmake index 6ca33b8..5ec4018 100644 --- a/Tests/RunCMake/ObjectLibrary/RunCMakeTest.cmake +++ b/Tests/RunCMake/ObjectLibrary/RunCMakeTest.cmake @@ -37,6 +37,10 @@ function (run_object_lib_build2 name) run_cmake_command(${name}-build ${CMAKE_COMMAND} --build .) endfunction () +if(NOT (RunCMake_GENERATOR STREQUAL "Xcode" AND "$ENV{CMAKE_OSX_ARCHITECTURES}" MATCHES "[;$]")) + run_object_lib_build(CheckTargetObjects) +endif() + run_object_lib_build(LinkObjLHSShared) run_object_lib_build(LinkObjLHSStatic) run_object_lib_build(LinkObjRHSShared) @@ -54,6 +58,7 @@ run_cmake(PostBuild) run_cmake(PreBuild) run_cmake(PreLink) + function(run_Dependencies) set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/Dependencies-build) set(RunCMake_TEST_NO_CLEAN 1) diff --git a/Tests/RunCMake/ObjectLibrary/check_object_files.cmake b/Tests/RunCMake/ObjectLibrary/check_object_files.cmake new file mode 100644 index 0000000..3c34229 --- /dev/null +++ b/Tests/RunCMake/ObjectLibrary/check_object_files.cmake @@ -0,0 +1,17 @@ + +if (NOT TARGET_OBJECTS) + message(SEND_ERROR "Object not passed as -DTARGET_OBJECTS") +endif() + +foreach(objlib_file IN LISTS objects) + message(STATUS "objlib_file: =${objlib_file}=") + + set(file_exists False) + if (EXISTS "${objlib_file}") + set(file_exists True) + endif() + + if (NOT file_exists) + message(SEND_ERROR "File \"${objlib_file}\" does not exist!${tried}") + endif() +endforeach() diff --git a/Tests/RunCMake/TargetObjects/NotObjlibTarget-stderr.txt b/Tests/RunCMake/TargetObjects/NotObjlibTarget-stderr.txt index a66794c..77c4afd 100644 --- a/Tests/RunCMake/TargetObjects/NotObjlibTarget-stderr.txt +++ b/Tests/RunCMake/TargetObjects/NotObjlibTarget-stderr.txt @@ -1,8 +1,9 @@ -CMake Error at NotObjlibTarget.cmake:3 \(file\): +CMake Error at NotObjlibTarget.cmake:[0-9]+ \(file\): Error evaluating generator expression: - \$ + \$ - Objects of target "StaticLib" referenced but is not an OBJECT library. + Objects of target "IFaceLib" referenced but is not an allowed library types + \(EXECUTABLE, STATIC, SHARED, MODULE, OBJECT\). Call Stack \(most recent call first\): - CMakeLists.txt:3 \(include\) + CMakeLists.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/TargetObjects/NotObjlibTarget.cmake b/Tests/RunCMake/TargetObjects/NotObjlibTarget.cmake index 3bb3e37..9fec369 100644 --- a/Tests/RunCMake/TargetObjects/NotObjlibTarget.cmake +++ b/Tests/RunCMake/TargetObjects/NotObjlibTarget.cmake @@ -1,3 +1,3 @@ -add_library(StaticLib empty.cpp) +add_library(IFaceLib INTERFACE ) -file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_output CONTENT $) +file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/test_output CONTENT $) -- cgit v0.12