File: AutogenDuplicateDependency.cmake

package info (click to toggle)
cmake 4.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152,456 kB
  • sloc: ansic: 403,896; cpp: 303,920; sh: 4,105; python: 3,583; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 111; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (38 lines) | stat: -rw-r--r-- 1,447 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
enable_language(CXX)

find_package(Qt${with_qt_version} REQUIRED COMPONENTS Core)

set(CMAKE_AUTOMOC ON)

file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp"
     CONTENT "void foo() {}")
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/main.cpp"
     CONTENT "int main() {return 0;}")

# Test case.
# App depends on Lib, which means App_autogen depends on Lib_autogen by default.
# Which means building App_autogen should trigger the generation of the
# fancy_generated.txt file, which is a dependency of Lib_autogen.

# Create a shared library and an executable.
add_library(Lib SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp")
add_executable(App "${CMAKE_CURRENT_BINARY_DIR}/main.cpp")

# Link Lib into App more than once. Previously this was not properly handled by AUTOGEN,
# which discarded the Lib_autogen dependency from App_autogen entirely, and the
# file was not generated.
foreach(i RANGE 1 ${LIB_LINK_COUNT})
  target_link_libraries(App PRIVATE Lib)
endforeach()

# Add a custom target that generates a file.
set(generated_file_path "${CMAKE_CURRENT_BINARY_DIR}/fancy_generated.txt")
add_custom_command(
    OUTPUT "${generated_file_path}"
    COMMAND ${CMAKE_COMMAND} -E touch "${generated_file_path}"
)
add_custom_target(generate_file DEPENDS "${generated_file_path}")

# Make sure the file is generated as part of building the Lib_autogen target.
set_target_properties(Lib PROPERTIES
    AUTOGEN_TARGET_DEPENDS generate_file)