d5961da4b1c8e001ba14afeaa9b8cfdf389564ad
[platform/upstream/cmake.git] / Help / guide / tutorial / Step12 / MathFunctions / CMakeLists.txt
1 # add the library that runs
2 add_library(MathFunctions MathFunctions.cxx)
3
4 # state that anybody linking to us needs to include the current source dir
5 # to find MathFunctions.h, while we don't.
6 target_include_directories(MathFunctions
7                            INTERFACE
8                             $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
9                             $<INSTALL_INTERFACE:include>
10                            )
11
12 # should we use our own math functions
13 option(USE_MYMATH "Use tutorial provided math implementation" ON)
14 if(USE_MYMATH)
15
16   target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
17
18   # first we add the executable that generates the table
19   add_executable(MakeTable MakeTable.cxx)
20   target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags)
21
22   # add the command to generate the source code
23   add_custom_command(
24     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
25     COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
26     DEPENDS MakeTable
27     )
28
29   # library that just does sqrt
30   add_library(SqrtLibrary STATIC
31               mysqrt.cxx
32               ${CMAKE_CURRENT_BINARY_DIR}/Table.h
33               )
34
35   # state that we depend on our binary dir to find Table.h
36   target_include_directories(SqrtLibrary PRIVATE
37                              ${CMAKE_CURRENT_BINARY_DIR}
38                              )
39
40   # state that SqrtLibrary need PIC when the default is shared libraries
41   set_target_properties(SqrtLibrary PROPERTIES
42                         POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
43                         )
44
45   target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
46   target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
47 endif()
48
49 target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
50
51 # define the symbol stating we are using the declspec(dllexport) when
52 # building on windows
53 target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")
54
55 # install rules
56 set(installable_libs MathFunctions tutorial_compiler_flags)
57 if(TARGET SqrtLibrary)
58   list(APPEND installable_libs SqrtLibrary)
59 endif()
60 install(TARGETS ${installable_libs}
61         EXPORT MathFunctionsTargets
62         DESTINATION lib)
63 install(FILES MathFunctions.h DESTINATION include)