CMakeLists.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #
  2. # Create exe.
  3. #
  4. string(APPEND CMAKE_CXX_FLAGS " -DTEST_CXX_FLAGS")
  5. string(APPEND CMAKE_C_FLAGS " -DTEST_C_FLAGS")
  6. # Create an imported target for if(TARGET) test below.
  7. add_library(ExeImportedTarget UNKNOWN IMPORTED)
  8. # Test if(TARGET) command.
  9. if(NOT TARGET CMakeTestLibrary)
  10. message(FATAL_ERROR "if(NOT TARGET CMakeTestLibrary) returned true!")
  11. endif()
  12. if(NOT TARGET ExeImportedTarget)
  13. message(FATAL_ERROR "if(NOT TARGET ExeImportedTarget) returned true!")
  14. endif()
  15. if(TARGET LibImportedTarget)
  16. message(FATAL_ERROR "if(TARGET LibImportedTarget) returned true!")
  17. endif()
  18. if(TARGET NotATarget)
  19. message(FATAL_ERROR "if(TARGET NotATarget) returned true!")
  20. endif()
  21. # Use LINK_LIBRARIES instead of TARGET_LINK_LIBRARIES to
  22. set(COMPLEX_LIBS CMakeTestLibrary;CMakeTestLibraryShared;CMakeTestCLibraryShared)
  23. link_libraries(${COMPLEX_LIBS})
  24. # Test forcing a .cxx file to not build.
  25. set_source_files_properties(complex_nobuild.cxx PROPERTIES
  26. HEADER_FILE_ONLY 1)
  27. # Test forcing a .c file to not build.
  28. # This makes sure a mixed language library is created
  29. # with header file only sources
  30. set_source_files_properties(complex_nobuild.c PROPERTIES
  31. HEADER_FILE_ONLY 1)
  32. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  33. add_executable(A A.cxx A.hh A.h A.txt)
  34. add_custom_command(OUTPUT Aout.h COMMAND A > Aout.h VERBATIM)
  35. add_executable(complex complex testcflags.c Aout.h)
  36. # Sub1/NameConflictTest.c Sub2/NameConflictTest.c)
  37. add_executable(complex.file complex.file.cxx complex_nobuild.cxx
  38. complex_nobuild.c)
  39. if (UNIX)
  40. target_link_libraries(complex ${CMAKE_DL_LIBS})
  41. else()
  42. if (NOT BORLAND)
  43. if(NOT MINGW)
  44. target_link_libraries(complex rpcrt4.lib)
  45. endif()
  46. endif()
  47. endif ()
  48. # Test linking to static lib when a shared lib has the same name.
  49. if(CMAKE_EXE_LINK_STATIC_CXX_FLAGS)
  50. add_definitions(-DCOMPLEX_TEST_LINK_STATIC)
  51. target_link_libraries(complex CMakeTestLinkStatic)
  52. endif()
  53. # can we get the path to a source file
  54. get_source_file_property(A_LOCATION A.cxx LOCATION)
  55. if ("${A_LOCATION}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/A.cxx")
  56. add_definitions(-DCMAKE_FOUND_ACXX)
  57. endif ()
  58. # get the directory parent
  59. get_directory_property(P_VALUE PARENT_DIRECTORY)
  60. if ("${P_VALUE}" STREQUAL "${CMAKE_SOURCE_DIR}")
  61. add_definitions(-DCMAKE_FOUND_PARENT)
  62. endif ()
  63. # get the stack of listfiles
  64. include(Included.cmake)
  65. if ("${LF_VALUE}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt;${CMAKE_CURRENT_SOURCE_DIR}/Included.cmake")
  66. add_definitions(-DCMAKE_FOUND_LISTFILE_STACK)
  67. endif ()
  68. # Test add/remove definitions.
  69. add_definitions(
  70. -DCOMPLEX_DEFINED_PRE
  71. -DCOMPLEX_DEFINED
  72. -DCOMPLEX_DEFINED_POST
  73. -DCOMPLEX_DEFINED
  74. )
  75. remove_definitions(-DCOMPLEX_DEFINED)
  76. # Test pre-build/pre-link/post-build rules for an executable.
  77. add_custom_command(TARGET complex PRE_BUILD
  78. COMMAND ${CREATE_FILE_EXE}
  79. ARGS "Executable/prebuild.txt"
  80. WORKING_DIRECTORY "${Complex_BINARY_DIR}")
  81. add_custom_command(TARGET complex PRE_BUILD
  82. COMMAND ${CREATE_FILE_EXE}
  83. ARGS "Executable/prelink.txt"
  84. WORKING_DIRECTORY "${Complex_BINARY_DIR}")
  85. add_custom_command(TARGET complex POST_BUILD
  86. COMMAND ${CREATE_FILE_EXE}
  87. ARGS "Executable/postbuild.txt"
  88. WORKING_DIRECTORY "${Complex_BINARY_DIR}")
  89. add_custom_command(TARGET complex POST_BUILD
  90. COMMAND ${CMAKE_COMMAND}
  91. ARGS -E copy
  92. "Executable/postbuild.txt"
  93. "Executable/postbuild2.txt"
  94. WORKING_DIRECTORY "${Complex_BINARY_DIR}")
  95. set_source_files_properties(complex
  96. COMPILE_FLAGS
  97. "-DFILE_HAS_EXTRA_COMPILE_FLAGS"
  98. #" -DFILE_DEFINE_STRING=\\\"hello\\\""
  99. OBJECT_DEPENDS ${Complex_BINARY_DIR}/cmTestGeneratedHeader.h
  100. )
  101. set_target_properties(complex PROPERTIES COMPILE_FLAGS "-DCOMPLEX_TARGET_FLAG")
  102. add_custom_command(
  103. TARGET complex
  104. SOURCE ${Complex_SOURCE_DIR}/cmTestGeneratedHeader.h.in
  105. COMMAND ${CMAKE_COMMAND}
  106. ARGS -E copy ${Complex_SOURCE_DIR}/cmTestGeneratedHeader.h.in
  107. ${Complex_BINARY_DIR}/cmTestGeneratedHeader.h
  108. OUTPUTS ${Complex_BINARY_DIR}/cmTestGeneratedHeader.h
  109. DEPENDS ${CMAKE_COMMAND}
  110. )
  111. # Test creating an executable that is not built by default.
  112. add_executable(notInAllExe EXCLUDE_FROM_ALL notInAllExe.cxx)
  113. target_link_libraries(notInAllExe notInAllLib)
  114. # Test user-value flag mapping for the VS IDE.
  115. if(MSVC)
  116. set_target_properties(notInAllExe PROPERTIES
  117. LINK_FLAGS "/NODEFAULTLIB:LIBC;LIBCMT;MSVCRT")
  118. endif()
  119. # Test creating a custom target that builds not-in-all targets.
  120. add_custom_target(notInAllCustom)
  121. add_dependencies(notInAllCustom notInAllExe)
  122. #
  123. # Output the files required by 'complex' to a file.
  124. #
  125. # This test has been moved to the 'required' subdir so that it
  126. # has no side-effects on the current Makefile (duplicated source file
  127. # due to source list expansion done twice).
  128. #
  129. add_subdirectory(Temp)
  130. if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_INCLUDE_SYSTEM_FLAG_CXX
  131. AND NOT XCODE) # XCODE is excluded due to #15687
  132. add_executable(testSystemDir testSystemDir.cxx)
  133. set_target_properties(testSystemDir PROPERTIES COMPILE_FLAGS "-Werror")
  134. endif()
  135. #
  136. # Extra coverage.Not used.
  137. #
  138. install_targets(/tmp complex)
  139. install_programs(/tmp complex)
  140. configure_file(
  141. ${Complex_SOURCE_DIR}/Executable/cmVersion.h.in
  142. ${Complex_BINARY_DIR}/cmVersion.h)
  143. source_group(A_GROUP ".cxx")
  144. source_group(B_GROUP REGULAR_EXPRESSION "cxx")
  145. source_group(C_GROUP FILES complex.cxx)
  146. file(WRITE ${Complex_BINARY_DIR}/A/libA.a "test")
  147. file(WRITE ${Complex_BINARY_DIR}/A/libC.a "test")
  148. file(WRITE ${Complex_BINARY_DIR}/B/libB.a "test")
  149. file(WRITE ${Complex_BINARY_DIR}/B/libA.a "test")
  150. file(WRITE ${Complex_BINARY_DIR}/C/libC.a "test")
  151. file(WRITE ${Complex_BINARY_DIR}/C/libB.a "test")