CMakeLists.txt 5.71 KB
Newer Older
1
#
2
# Create exe.
3
#
4 5
string(APPEND CMAKE_CXX_FLAGS " -DTEST_CXX_FLAGS")
string(APPEND CMAKE_C_FLAGS " -DTEST_C_FLAGS")
6

Brad King's avatar
Brad King committed
7
# Create an imported target for if(TARGET) test below.
8
add_library(ExeImportedTarget UNKNOWN IMPORTED)
Brad King's avatar
Brad King committed
9 10

# Test if(TARGET) command.
11 12
if(NOT TARGET CMakeTestLibrary)
  message(FATAL_ERROR "if(NOT TARGET CMakeTestLibrary) returned true!")
13
endif()
14 15
if(NOT TARGET ExeImportedTarget)
  message(FATAL_ERROR "if(NOT TARGET ExeImportedTarget) returned true!")
16
endif()
17 18
if(TARGET LibImportedTarget)
  message(FATAL_ERROR "if(TARGET LibImportedTarget) returned true!")
19
endif()
20 21
if(TARGET NotATarget)
  message(FATAL_ERROR "if(TARGET NotATarget) returned true!")
22
endif()
Brad King's avatar
Brad King committed
23

24
# Use LINK_LIBRARIES instead of TARGET_LINK_LIBRARIES to
25 26
set(COMPLEX_LIBS CMakeTestLibrary;CMakeTestLibraryShared;CMakeTestCLibraryShared)
link_libraries(${COMPLEX_LIBS})
27

28
# Test forcing a .cxx file to not build.
29
set_source_files_properties(complex_nobuild.cxx PROPERTIES
30 31
  HEADER_FILE_ONLY 1)

32 33 34
# Test forcing a .c file to not build.
# This makes sure a mixed language library is created
# with header file only sources
35
set_source_files_properties(complex_nobuild.c PROPERTIES
36 37
  HEADER_FILE_ONLY 1)

38 39 40 41
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(A A.cxx A.hh A.h A.txt)
add_custom_command(OUTPUT Aout.h COMMAND A > Aout.h VERBATIM)
add_executable(complex complex testcflags.c Aout.h)
42
#  Sub1/NameConflictTest.c Sub2/NameConflictTest.c)
43
add_executable(complex.file complex.file.cxx complex_nobuild.cxx
44
  complex_nobuild.c)
45

46 47
if (UNIX)
  target_link_libraries(complex ${CMAKE_DL_LIBS})
48
else()
49 50 51
  if (NOT BORLAND)
    if(NOT MINGW)
      target_link_libraries(complex rpcrt4.lib)
52 53 54
    endif()
  endif()
endif ()
Ken Martin's avatar
Ken Martin committed
55

56
# Test linking to static lib when a shared lib has the same name.
57 58 59
if(CMAKE_EXE_LINK_STATIC_CXX_FLAGS)
  add_definitions(-DCOMPLEX_TEST_LINK_STATIC)
  target_link_libraries(complex CMakeTestLinkStatic)
60
endif()
61

62
# can we get the path to a source file
63 64 65
get_source_file_property(A_LOCATION A.cxx LOCATION)
if ("${A_LOCATION}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/A.cxx")
  add_definitions(-DCMAKE_FOUND_ACXX)
66
endif ()
67 68

# get the directory parent
69 70 71
get_directory_property(P_VALUE PARENT_DIRECTORY)
if ("${P_VALUE}" STREQUAL "${CMAKE_SOURCE_DIR}")
  add_definitions(-DCMAKE_FOUND_PARENT)
72
endif ()
73 74

# get the stack of listfiles
75 76 77
include(Included.cmake)
if ("${LF_VALUE}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt;${CMAKE_CURRENT_SOURCE_DIR}/Included.cmake")
  add_definitions(-DCMAKE_FOUND_LISTFILE_STACK)
78
endif ()
79

80
# Test add/remove definitions.
81
add_definitions(
82 83 84 85 86
  -DCOMPLEX_DEFINED_PRE
  -DCOMPLEX_DEFINED
  -DCOMPLEX_DEFINED_POST
  -DCOMPLEX_DEFINED
  )
87
remove_definitions(-DCOMPLEX_DEFINED)
88

89
# Test pre-build/pre-link/post-build rules for an executable.
90
add_custom_command(TARGET complex PRE_BUILD
91
                   COMMAND ${CREATE_FILE_EXE}
92 93
                   ARGS "Executable/prebuild.txt"
                   WORKING_DIRECTORY "${Complex_BINARY_DIR}")
94
add_custom_command(TARGET complex PRE_LINK
95
                   COMMAND ${CREATE_FILE_EXE}
96 97
                   ARGS "Executable/prelink.txt"
                   WORKING_DIRECTORY "${Complex_BINARY_DIR}")
98
add_custom_command(TARGET complex POST_BUILD
99
                   COMMAND ${CREATE_FILE_EXE}
100 101
                   ARGS "Executable/postbuild.txt"
                   WORKING_DIRECTORY "${Complex_BINARY_DIR}")
102
add_custom_command(TARGET complex POST_BUILD
103 104
                   COMMAND ${CMAKE_COMMAND}
                   ARGS -E copy
105 106 107
                        "Executable/postbuild.txt"
                        "Executable/postbuild2.txt"
                   WORKING_DIRECTORY "${Complex_BINARY_DIR}")
108

109
set_source_files_properties(complex
110
  COMPILE_FLAGS
111 112
  "-DFILE_HAS_EXTRA_COMPILE_FLAGS"
  #" -DFILE_DEFINE_STRING=\\\"hello\\\""
113 114
  OBJECT_DEPENDS ${Complex_BINARY_DIR}/cmTestGeneratedHeader.h
)
115 116
set_target_properties(complex PROPERTIES COMPILE_FLAGS "-DCOMPLEX_TARGET_FLAG")
add_custom_command(
117 118 119 120 121 122 123 124 125
  TARGET complex
  SOURCE ${Complex_SOURCE_DIR}/cmTestGeneratedHeader.h.in
  COMMAND ${CMAKE_COMMAND}
  ARGS -E copy ${Complex_SOURCE_DIR}/cmTestGeneratedHeader.h.in
               ${Complex_BINARY_DIR}/cmTestGeneratedHeader.h
  OUTPUTS ${Complex_BINARY_DIR}/cmTestGeneratedHeader.h
  DEPENDS ${CMAKE_COMMAND}
)

126
# Test creating an executable that is not built by default.
127 128
add_executable(notInAllExe EXCLUDE_FROM_ALL notInAllExe.cxx)
target_link_libraries(notInAllExe notInAllLib)
129

130
# Test user-value flag mapping for the VS IDE.
131 132
if(MSVC)
  set_target_properties(notInAllExe PROPERTIES
133
    LINK_FLAGS "/NODEFAULTLIB:LIBC;LIBCMT;MSVCRT")
134
endif()
135

136
# Test creating a custom target that builds not-in-all targets.
137 138
add_custom_target(notInAllCustom)
add_dependencies(notInAllCustom notInAllExe)
139

Sebastien Barre's avatar
Sebastien Barre committed
140 141 142
#
# Output the files required by 'complex' to a file.
#
143 144 145 146
# This test has been moved to the 'required' subdir so that it
# has no side-effects on the current Makefile (duplicated source file
# due to source list expansion done twice).
#
147
add_subdirectory(Temp)
Sebastien Barre's avatar
Sebastien Barre committed
148

149 150
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_INCLUDE_SYSTEM_FLAG_CXX
   AND NOT XCODE) # XCODE is excluded due to #15687
151 152
  add_executable(testSystemDir testSystemDir.cxx)
  set_target_properties(testSystemDir PROPERTIES COMPILE_FLAGS "-Werror")
153
endif()
154

155
#
156
# Extra coverage.Not used.
157
#
158 159
install_targets(/tmp complex)
install_programs(/tmp complex)
160

161
configure_file(
Ken Martin's avatar
Ken Martin committed
162 163 164
  ${Complex_SOURCE_DIR}/Executable/cmVersion.h.in
  ${Complex_BINARY_DIR}/cmVersion.h)

165 166 167
source_group(A_GROUP ".cxx")
source_group(B_GROUP REGULAR_EXPRESSION "cxx")
source_group(C_GROUP FILES complex.cxx)
Bill Hoffman's avatar
Bill Hoffman committed
168

169 170 171 172 173 174
file(WRITE ${Complex_BINARY_DIR}/A/libA.a "test")
file(WRITE ${Complex_BINARY_DIR}/A/libC.a "test")
file(WRITE ${Complex_BINARY_DIR}/B/libB.a "test")
file(WRITE ${Complex_BINARY_DIR}/B/libA.a "test")
file(WRITE ${Complex_BINARY_DIR}/C/libC.a "test")
file(WRITE ${Complex_BINARY_DIR}/C/libB.a "test")