Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

cmake - Postpone making custom target until install

I have something like this in my project:

add_custom_command(OUTPUT somefile)
add_custom_target(tgt ALL DEPENDS somefile)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/somefile DESTINATION somedir)

This works OK, but my command is being run during make because of ALL keyword in add_custom_target(). What i want is to make CMake to run this command only when make install is issued, not during build.

If i remove ALL keyword, whole target is not being built by default, so somefile is not produced and make install fails.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A possible solution is to have the make install command invoke the make tgt as a side effect. This can be done by using the CODE signature of the install command:

add_custom_command(OUTPUT somefile)
add_custom_target(tgt DEPENDS somefile)

install(CODE "execute_process(COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_CURRENT_BINARY_DIR}" --target tgt)")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/somefile DESTINATION somedir)

The execute_process invokes cmake to build the target tgt before somefile is installed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...