Skip to content

Commit 63dbfac

Browse files
author
Chris Cummins
authored
Merge pull request #675 from sogartar/fix-cmake-custom-target-dependencies
CMake: fix custom target dependencies
2 parents 052cad7 + c25d3ad commit 63dbfac

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

build_tools/cmake/cg_filegroup.cmake

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
55

6+
include_guard(GLOBAL)
7+
include(cg_target_outputs)
8+
69
function(cg_filegroup)
710
cmake_parse_arguments(_ARG "PUBLIC" "NAME" "FILES;DEPENDS" ${ARGN})
811
rename_bazel_targets(_NAME "${_ARG_NAME}")
912
add_custom_target(${_NAME})
1013

14+
rename_bazel_targets(_DEPS "${_ARG_DEPENDS}")
15+
cg_target_outputs(TARGETS ${_DEPS} RESULT _DEPS_OUTPUTS)
16+
17+
unset(_OUTPUTS)
18+
1119
foreach(FILE_ ${_ARG_FILES})
1220
if(IS_ABSOLUTE "${FILE_}")
1321
set(_INPUT_PATH "${FILE_}")
@@ -29,22 +37,19 @@ function(cg_filegroup)
2937
COMMAND
3038
${CMAKE_COMMAND} -E create_symlink "${_INPUT_PATH}"
3139
"${_OUTPUT_PATH}"
32-
DEPENDS "${_INPUT_PATH}"
40+
DEPENDS "${_INPUT_PATH}" ${_DEPS_OUTPUTS}
3341
)
3442
endif()
3543
add_custom_target(${_TARGET} DEPENDS "${_OUTPUT_PATH}")
3644
endif()
45+
list(APPEND _OUTPUTS "${_OUTPUT_PATH}")
3746

3847
add_dependencies(${_NAME} ${_TARGET})
3948
endforeach()
4049

4150
if(_ARG_DEPENDS)
42-
rename_bazel_targets(_DEPS "${_ARG_DEPENDS}")
4351
add_dependencies(${_NAME} ${_DEPS})
4452
endif()
4553

46-
set_target_properties(
47-
${_NAME}
48-
PROPERTIES IS_FILEGROUP TRUE OUTPUTS "${_SRCS}"
49-
)
54+
set_property(TARGET ${_NAME} PROPERTY OUTPUTS ${_OUTPUTS})
5055
endfunction()

build_tools/cmake/cg_genrule.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include_guard(GLOBAL)
77

88
include(CMakeParseArguments)
99
include(cg_macros)
10+
include(cg_target_outputs)
1011

1112
# cg_genrule()
1213
#
@@ -55,10 +56,11 @@ function(cg_genrule)
5556
string(REPLACE "$(@D)" "${_OUTS_DIR}" _CMD "${_CMD}")
5657

5758
if(_OUTS)
59+
cg_target_outputs(TARGETS ${_DEPS} RESULT _DEPS_OUTPUTS)
5860
add_custom_command(
5961
OUTPUT ${_OUTS}
6062
COMMAND bash -c "${_CMD}"
61-
DEPENDS ${_DEPS} ${_SRCS}
63+
DEPENDS ${_DEPS} ${_DEPS_OUTPUTS} ${_SRCS}
6264
VERBATIM
6365
USES_TERMINAL
6466
)
@@ -83,7 +85,7 @@ function(cg_genrule)
8385
)
8486
endif()
8587

86-
set_target_properties(${_NAME} PROPERTIES OUTPUTS "${_OUTS}")
88+
set_property(TARGET ${_NAME} PROPERTY OUTPUTS ${_OUTS})
8789

8890
list(LENGTH _OUTS _OUTS_LENGTH)
8991
if(_OUTS_LENGTH EQUAL "1")

build_tools/cmake/cg_py_library.cmake

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function(cg_py_library)
4242

4343
# TODO(boian): remove this renaming when call sites do not include ":" in target dependency names
4444
rename_bazel_targets(_RULE_DEPS "${_RULE_DEPS}")
45+
cg_target_outputs(TARGETS ${_RULE_DEPS} RESULT _DEPS_OUTPUTS)
4546

4647
# Prefix the library with the package name, so we get: cg_package_name.
4748
rename_bazel_targets(_NAME "${_RULE_NAME}")
@@ -64,19 +65,24 @@ function(cg_py_library)
6465
COMMAND
6566
${CMAKE_COMMAND} -E create_symlink
6667
"${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}" "${_SRC_BIN_PATH}"
67-
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}"
68+
DEPENDS
69+
"${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}"
70+
${_RULE_DEPS}
71+
${_DEPS_OUTPUTS}
72+
${_RULE_GENERATED_SRCS}
6873
VERBATIM
6974
)
7075
list(APPEND _BIN_PATHS "${_SRC_BIN_PATH}")
7176
endforeach()
7277

7378
list(APPEND _BIN_PATHS ${_RULE_GENERATED_SRCS})
74-
7579
set(_DEPS ${_RULE_DEPS} ${_BIN_PATHS})
7680
add_custom_target(${_NAME} ALL DEPENDS ${_DEPS})
7781

7882
cg_add_data_dependencies(NAME ${_RULE_NAME} DATA ${_RULE_DATA})
7983

84+
set_property(TARGET ${_NAME} PROPERTY OUTPUTS ${_BIN_PATHS})
85+
8086
# If only one src file set the LOCATION target property to point to it.
8187
list(LENGTH _BIN_PATHS _BIN_PATHS_LENGTH)
8288
if(_BIN_PATHS_LENGTH EQUAL "1")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
include_guard(GLOBAL)
7+
include(CMakeParseArguments)
8+
9+
function(cg_target_outputs)
10+
cmake_parse_arguments(ARG "" "RESULT" "TARGETS" ${ARGN})
11+
unset(RES_)
12+
foreach(TARGET_ ${ARG_TARGETS})
13+
list(APPEND RES_ $<TARGET_PROPERTY:${TARGET_},OUTPUTS>)
14+
endforeach()
15+
set("${ARG_RESULT}" ${RES_} PARENT_SCOPE)
16+
endfunction()

build_tools/cmake/grpc.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include_guard(GLOBAL)
1010
include(CMakeParseArguments)
1111
include(cg_macros)
1212
include(cg_py_library)
13+
include(cg_target_outputs)
1314
include(protobuf)
1415

1516
function(get_cc_grpc_proto_out_files _PROTO_FILENAME _RESULT)
@@ -43,6 +44,7 @@ function(cc_grpc_library)
4344
endif()
4445

4546
rename_bazel_targets(_DEPS "${_RULE_DEPS}")
47+
cg_target_outputs(TARGETS ${_DEPS} RESULT _DEPS_OUTPUTS)
4648
rename_bazel_targets(_NAME "${_RULE_NAME}")
4749
rename_bazel_targets(_SRCS "${_RULE_SRCS}")
4850

@@ -84,6 +86,7 @@ function(cc_grpc_library)
8486
"${_DESCRIPTOR_SET_FILE}"
8587
"${_PROTO_FILE}"
8688
${_DEPS}
89+
${_DEPS_OUTPUTS}
8790
VERBATIM
8891
)
8992

@@ -119,6 +122,7 @@ function(py_grpc_library)
119122
cmake_parse_arguments(_RULE "" "NAME;SRCS" "DEPS" ${ARGN})
120123

121124
rename_bazel_targets(_DEPS "${_RULE_DEPS}")
125+
cg_target_outputs(TARGETS ${_DEPS} RESULT _DEPS_OUTPUTS)
122126
rename_bazel_targets(_SRCS "${_RULE_SRCS}")
123127

124128
get_target_property(_DESCRIPTOR_SET_FILE ${_SRCS} PROTO_DESCRIPTOR_SETS)
@@ -150,6 +154,7 @@ function(py_grpc_library)
150154
"${_DESCRIPTOR_SET_FILE}"
151155
"${_PROTO_FILE}"
152156
${_DEPS}
157+
${_DEPS_OUTPUTS}
153158
VERBATIM
154159
)
155160

compiler_gym/util/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ cg_py_library(
3232
"thread_pool.py"
3333
"timer.py"
3434
"truncate.py"
35-
GENERATED_SRCS
36-
"$<TARGET_PROPERTY:compiler_gym__util__make_version,LOCATION>"
3735
DEPS
3836
make_version
3937
compiler_gym::errors::errors

0 commit comments

Comments
 (0)