CMake custom target doesn't build

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

CMake custom target doesn't build

Gil Moses

 

 

Hi,

I’m posting this after getting no useful reply in StackOverflow: https://stackoverflow.com/questions/50198141/cmake-custom-target-doesnt-build.

 

Using CMake 2.8.8, I'm building two targets using:

add_library(tgt1 SHARED a.cpp)

add_library(tgt2 SHARED b.cpp)

After both are built, I need to run a post build step that depends on both targets. I tried many combinations of the following but with no success:

add_custom_target(final_tgt DEPENDS tgt1 tgt2)

add_custom_command(TARGET final_tgt POST_BUILD COMMAND <command> ARGS <args>)

The final target would simply not build, even though its build.make contains the custom command.

Tried to use ALL for the custom target, however make attempts to build it first while missing the first targets.

And I can't use an add_library or add_executable for the final target, since they require specifying source files.

What is the correct way to do it?

===================================

Edit: below is a minimal verifiable source code. What it attempts to do is to compile code (for Mac) in two architectures and as a post-build to create a universal binary using lipo:

cmake_minimum_required(VERSION 2.8)

set(icpc_req_path "/usr/local/bin/icpc-16.0.146")

 

set(CMAKE_CXX_COMPILER "${icpc_req_path}")

 

project("CMakeTest")

set(SOURCE_FILES a.cpp)

 

set (TARGET_NAME "TGT")

set(TARGETS "")

set(ARCHITECTURES i386 x86_64)

 

foreach(ar ${ARCHITECTURES})

    set(CMAKE_CXX_FLAGS_RELEASE "")

    set(CMAKE_CXX_FLAGS_DEBUG "")

    set(CMAKE_CXX_FLAGS "")

 

    add_library(TGT_${ar} SHARED ${SOURCE_FILES})

    set_target_properties(${TARGET_NAME}_${ar} PROPERTIES COMPILE_FLAGS "-arch ${ar} -xSSE3")

    set_target_properties(${TARGET_NAME}_${ar} PROPERTIES LINK_FLAGS "-arch ${ar}")

    set(TARGETS "${TARGETS};lib${TARGET_NAME}_${ar}.dylib")

endforeach(ar)

 

message("Targets: ${TARGETS}")

add_custom_target(${TARGET_NAME} DEPENDS ${TARGETS})

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND "lipo" ARGS "-create" ${TARGETS} "-output" "${TARGET_NAME}.dylib")

 

And the contents of a.cpp is:

int main(){}

 

Thanks,

Gil.

 


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: CMake custom target doesn't build

Petr Kmoch
Hi Gil.

The DEPENDS argument of add_custom_target() is for specifying dependency *files*, not *targets*.

As the docs (https://cmake.org/cmake/help/latest/command/add_custom_target.html) say, what you need is add_dependencies().

Also note that you can add the COMMAND directly to the custom target, no need for a post-build step:

add_custom_target(final_tgt ALL

  COMMAND <command> ARGS <args>

)

add_dependencies(final_tgt tgt1 tgt2)

This way, the custom target will depend on tgt1 and tgt2, and thus only build (run its command) after they're up to date.

Petr

On 14 May 2018 at 15:22, Gil Moses <[hidden email]> wrote:

 

 

Hi,

I’m posting this after getting no useful reply in StackOverflow: https://stackoverflow.com/questions/50198141/cmake-custom-target-doesnt-build.

 

Using CMake 2.8.8, I'm building two targets using:

add_library(tgt1 SHARED a.cpp)

add_library(tgt2 SHARED b.cpp)

After both are built, I need to run a post build step that depends on both targets. I tried many combinations of the following but with no success:

add_custom_target(final_tgt DEPENDS tgt1 tgt2)

add_custom_command(TARGET final_tgt POST_BUILD COMMAND <command> ARGS <args>)

The final target would simply not build, even though its build.make contains the custom command.

Tried to use ALL for the custom target, however make attempts to build it first while missing the first targets.

And I can't use an add_library or add_executable for the final target, since they require specifying source files.

What is the correct way to do it?

===================================

Edit: below is a minimal verifiable source code. What it attempts to do is to compile code (for Mac) in two architectures and as a post-build to create a universal binary using lipo:

cmake_minimum_required(VERSION 2.8)

set(icpc_req_path "/usr/local/bin/icpc-16.0.146")

 

set(CMAKE_CXX_COMPILER "${icpc_req_path}")

 

project("CMakeTest")

set(SOURCE_FILES a.cpp)

 

set (TARGET_NAME "TGT")

set(TARGETS "")

set(ARCHITECTURES i386 x86_64)

 

foreach(ar ${ARCHITECTURES})

    set(CMAKE_CXX_FLAGS_RELEASE "")

    set(CMAKE_CXX_FLAGS_DEBUG "")

    set(CMAKE_CXX_FLAGS "")

 

    add_library(TGT_${ar} SHARED ${SOURCE_FILES})

    set_target_properties(${TARGET_NAME}_${ar} PROPERTIES COMPILE_FLAGS "-arch ${ar} -xSSE3")

    set_target_properties(${TARGET_NAME}_${ar} PROPERTIES LINK_FLAGS "-arch ${ar}")

    set(TARGETS "${TARGETS};lib${TARGET_NAME}_${ar}.dylib")

endforeach(ar)

 

message("Targets: ${TARGETS}")

add_custom_target(${TARGET_NAME} DEPENDS ${TARGETS})

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND "lipo" ARGS "-create" ${TARGETS} "-output" "${TARGET_NAME}.dylib")

 

And the contents of a.cpp is:

int main(){}

 

Thanks,

Gil.

 


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: CMake custom target doesn't build

Gil Moses

Petr,

That worked for me, thanks!!!

Gil.


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake