-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmake: fix problem that go_library is never rebuilt. #2954
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,8 +290,22 @@ function(go_library TARGET_NAME) | |
set(${TARGET_NAME}_LIB_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}${TARGET_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}" CACHE STRING "output library name for target ${TARGET_NAME}") | ||
endif() | ||
|
||
# Add dummy code to support `make target_name` under Terminal Command | ||
set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_dummy.c) | ||
|
||
# This custom command will always run since it depends on a not | ||
# existing file. | ||
add_custom_command( | ||
OUTPUT dummy_rebulid_${TARGET_NAME} | ||
COMMAND cmake -E touch ${dummyfile} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that being built every time? Even if there no changes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I thought it's better to rebuild even there is no change than not rebuild when there is change. It's hard to track if the library source to be built changed or not, since the files may depend on other files that have changed located in other directories. The good news is Go compiler caches the build result for all dependencies when they have not changed. |
||
) | ||
# Create a custom target that depends on the custom command output | ||
# file, so the custom command can be referenced as a dependency by | ||
# `add_dependencies`. | ||
add_custom_target(rebuild_${TARGET_NAME} | ||
DEPENDS dummy_rebulid_${TARGET_NAME} | ||
) | ||
|
||
# Add dummy code to support `make target_name` under Terminal Command | ||
file(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";") | ||
if (go_library_SHARED OR go_library_shared) | ||
add_library(${TARGET_NAME} SHARED ${dummyfile}) | ||
|
@@ -302,6 +316,12 @@ function(go_library TARGET_NAME) | |
add_dependencies(${TARGET_NAME} ${go_library_DEPS}) | ||
endif(go_library_DEPS) | ||
|
||
# The "source file" of the library is `${dummyfile}` which never | ||
# change, so the target will never rebuild. Make the target depends | ||
# on the custom command that touches the library "source file", so | ||
# rebuild will always happen. | ||
add_dependencies(${TARGET_NAME} rebuild_${TARGET_NAME}) | ||
|
||
set(${TARGET_NAME}_LIB_PATH "${CMAKE_CURRENT_BINARY_DIR}/${${TARGET_NAME}_LIB_NAME}" CACHE STRING "output library path for target ${TARGET_NAME}") | ||
|
||
file(GLOB GO_SOURCE RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.go") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naive question: why add a custom command since we already have
file(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
below?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding
file
will create a file whencmake
is called, during the Makefile generation process. But it will never be called whenmake
is called. This has the problem that after the first time building, go_library never gets build again even the source code changed.In contrast, the custom command is called when
make
is called, forcing the rebuild.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Thanks! No rebuild is due to we build go libs by
add_custom_command
, somake
will not track the source file changes.