Skip to content

Commit 7a0e772

Browse files
authored
Merge pull request #2954 from helinwang/cmake_rebuild
cmake: fix problem that go_library is never rebuilt.
2 parents e3b27d1 + 3d4e808 commit 7a0e772

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

cmake/generic.cmake

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,22 @@ function(go_library TARGET_NAME)
290290
set(${TARGET_NAME}_LIB_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}${TARGET_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}" CACHE STRING "output library name for target ${TARGET_NAME}")
291291
endif()
292292

293-
# Add dummy code to support `make target_name` under Terminal Command
294293
set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_dummy.c)
294+
295+
# This custom command will always run since it depends on a not
296+
# existing file.
297+
add_custom_command(
298+
OUTPUT dummy_rebulid_${TARGET_NAME}
299+
COMMAND cmake -E touch ${dummyfile}
300+
)
301+
# Create a custom target that depends on the custom command output
302+
# file, so the custom command can be referenced as a dependency by
303+
# `add_dependencies`.
304+
add_custom_target(rebuild_${TARGET_NAME}
305+
DEPENDS dummy_rebulid_${TARGET_NAME}
306+
)
307+
308+
# Add dummy code to support `make target_name` under Terminal Command
295309
file(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
296310
if (go_library_SHARED OR go_library_shared)
297311
add_library(${TARGET_NAME} SHARED ${dummyfile})
@@ -302,6 +316,12 @@ function(go_library TARGET_NAME)
302316
add_dependencies(${TARGET_NAME} ${go_library_DEPS})
303317
endif(go_library_DEPS)
304318

319+
# The "source file" of the library is `${dummyfile}` which never
320+
# change, so the target will never rebuild. Make the target depends
321+
# on the custom command that touches the library "source file", so
322+
# rebuild will always happen.
323+
add_dependencies(${TARGET_NAME} rebuild_${TARGET_NAME})
324+
305325
set(${TARGET_NAME}_LIB_PATH "${CMAKE_CURRENT_BINARY_DIR}/${${TARGET_NAME}_LIB_NAME}" CACHE STRING "output library path for target ${TARGET_NAME}")
306326

307327
file(GLOB GO_SOURCE RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.go")

0 commit comments

Comments
 (0)