bb4b92c1b681fcac0f19a714ad00b38ac739e7f2
[platform/upstream/cmake.git] / Tests / IncludeDirectories / CMakeLists.txt
1 cmake_minimum_required (VERSION 2.6)
2
3 if(POLICY CMP0129)
4   cmake_policy(SET CMP0129 NEW)
5 endif()
6
7 project(IncludeDirectories)
8 if (((CMAKE_C_COMPILER_ID STREQUAL GNU AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.4)
9     OR (CMAKE_C_COMPILER_ID STREQUAL Clang AND NOT "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC")
10     OR CMAKE_C_COMPILER_ID STREQUAL NVHPC
11     OR CMAKE_C_COMPILER_ID STREQUAL AppleClang
12     OR CMAKE_C_COMPILER_ID STREQUAL LCC
13     OR ("x${CMAKE_C_COMPILER_ID}" STREQUAL "xMSVC" AND
14        CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "19.29.30036.3"))
15     AND (CMAKE_GENERATOR STREQUAL "Unix Makefiles"
16       OR CMAKE_GENERATOR STREQUAL "Ninja"
17       OR (CMAKE_GENERATOR STREQUAL "Xcode" AND NOT XCODE_VERSION VERSION_LESS 6.0)
18       OR CMAKE_GENERATOR MATCHES "Visual Studio"))
19   if ("x${CMAKE_C_COMPILER_ID}" STREQUAL "xMSVC")
20     set(run_sys_includes_test 1)
21   else ()
22     include(CheckCXXCompilerFlag)
23     check_cxx_compiler_flag(-Wunused-variable run_sys_includes_test)
24     if(run_sys_includes_test)
25       # The Bullseye wrapper appears to break the -isystem effect.
26       execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE out ERROR_VARIABLE out)
27       if("x${out}" MATCHES "Bullseye")
28         set(run_sys_includes_test 0)
29       endif()
30     endif()
31   endif()
32   if (run_sys_includes_test)
33     add_subdirectory(SystemIncludeDirectories)
34     add_subdirectory(SystemIncludeDirectoriesPerLang)
35   endif()
36 endif()
37
38 file(WRITE ${CMAKE_BINARY_DIR}/Flags/Flags.h
39 "//Flags.h
40 ")
41 file(WRITE ${CMAKE_BINARY_DIR}/IncDir/IncDir.h
42 "//IncDir.h
43 ")
44 file(WRITE ${CMAKE_BINARY_DIR}/SrcProp/SrcProp.h
45 "//SrcProp.h
46 ")
47 file(WRITE ${CMAKE_BINARY_DIR}/TarProp/TarProp.h
48 "//TarProp.h
49 ")
50
51 # default to testing with full path
52 # some compilers can not handle the escape for directories
53 # with spaces in them.
54 set(USE_FULLPATH TRUE)
55 if(WATCOM OR MSVC60)
56   set(USE_FULLPATH FALSE)
57 endif()
58 if(USE_FULLPATH)
59   string(APPEND CMAKE_CXX_FLAGS " \"-I${CMAKE_BINARY_DIR}/Flags\"")
60 else()
61   string(APPEND CMAKE_CXX_FLAGS " -IFlags")
62 endif()
63
64 include_directories(${CMAKE_BINARY_DIR}/IncDir)
65 if(USE_FULLPATH)
66   set_source_files_properties(main.cpp PROPERTIES COMPILE_FLAGS
67     "\"-I${CMAKE_BINARY_DIR}/SrcProp\"")
68 else()
69   set_source_files_properties(main.cpp PROPERTIES COMPILE_FLAGS
70     "-ISrcProp")
71 endif()
72
73 add_executable(IncludeDirectories main.cpp)
74
75 if(USE_FULLPATH)
76   set_target_properties(IncludeDirectories
77     PROPERTIES COMPILE_FLAGS "\"-I${CMAKE_BINARY_DIR}/TarProp\"")
78 else()
79   set_target_properties(IncludeDirectories
80     PROPERTIES COMPILE_FLAGS "-ITarProp")
81 endif()
82
83 # Test escaping of special characters in include directory paths.
84 set(special_chars "~@&{}()!'")
85 if(NOT CMAKE_GENERATOR MATCHES "(Unix|MinGW|MSYS) Makefiles")
86   # when compiler is used for dependencies, special characters for make are not escaped
87   string(APPEND special_chars "%")
88 endif()
89 if(NOT CMAKE_GENERATOR STREQUAL "Watcom WMake")
90   # Watcom seems to have no way to encode these characters.
91   string(APPEND special_chars "#=[]")
92 endif()
93 if(NOT (MINGW AND CMAKE_GENERATOR MATCHES "(Unix|MSYS) Makefiles"))
94   # FIXME: Dependencies work but command-line generation does not handle '$'.
95   string(APPEND special_chars "$")
96 endif()
97 if(NOT CMAKE_GENERATOR MATCHES "(Borland|NMake) Makefiles")
98   # NMake and Borland seem to have no way to encode a single '^'.
99   string(APPEND special_chars "^")
100 endif()
101 if(NOT CMAKE_GENERATOR MATCHES "Visual Studio 9 2008|Watcom WMake")
102   # The vcproj format separates values with ','.
103   string(APPEND special_chars ",")
104 endif()
105 if(NOT WIN32 AND NOT CYGWIN)
106   string(APPEND special_chars "*?<>")
107 endif()
108 set(special_dir "${CMAKE_CURRENT_BINARY_DIR}/special-${special_chars}-include")
109 file(WRITE "${special_dir}/SpecialDir.h" "#define SPECIAL_DIR_H\n")
110 target_include_directories(IncludeDirectories PRIVATE "${special_dir}")
111 target_compile_definitions(IncludeDirectories PRIVATE INCLUDE_SPECIAL_DIR)
112
113 if(MAKE_SUPPORTS_SPACES)
114   set(special_space_dir "${CMAKE_CURRENT_BINARY_DIR}/special-space ${special_chars}-include")
115   file(WRITE "${special_space_dir}/SpecialSpaceDir.h" "#define SPECIAL_SPACE_DIR_H\n")
116   target_include_directories(IncludeDirectories PRIVATE "${special_space_dir}")
117   target_compile_definitions(IncludeDirectories PRIVATE INCLUDE_SPECIAL_SPACE_DIR)
118 endif()
119
120 add_library(ordertest ordertest.cpp)
121 target_include_directories(ordertest SYSTEM PUBLIC SystemIncludeDirectories/systemlib)
122 target_include_directories(ordertest PUBLIC SystemIncludeDirectories/userlib)
123
124 add_library(ordertest2 ordertest.cpp)
125 target_include_directories(ordertest2 SYSTEM PRIVATE SystemIncludeDirectories/systemlib)
126 target_link_libraries(ordertest2 PRIVATE ordertest2_userlib)
127 add_library(ordertest2_userlib INTERFACE IMPORTED)
128 target_include_directories(ordertest2_userlib INTERFACE SystemIncludeDirectories/userlib)
129 set_property(TARGET ordertest2_userlib PROPERTY IMPORTED_NO_SYSTEM 1)
130
131 add_subdirectory(StandardIncludeDirectories)
132 add_subdirectory(TargetIncludeDirectories)
133
134 set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR}")
135 get_property(propContent DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
136 if (NOT propContent STREQUAL "${CMAKE_BINARY_DIR}")
137   message(SEND_ERROR "Setting DIRECTORY property failed.")
138 endif()
139 set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
140 get_property(propContentAfter DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
141 if (NOT propContentAfter STREQUAL "")
142   message(SEND_ERROR "Clearing DIRECTORY property failed.")
143 endif()
144
145 add_library(empty_entry_test SHARED empty.cpp)
146 set_target_properties(empty_entry_test PROPERTIES INCLUDE_DIRECTORIES "")
147 include_directories(/one/two
148   " "
149   "  "
150 )
151 get_target_property(incs empty_entry_test INCLUDE_DIRECTORIES)
152 if (NOT incs STREQUAL ";/one/two")
153   message(SEND_ERROR "Empty include_directories entry was not ignored.")
154 endif()
155
156 if(NOT CMAKE_GENERATOR STREQUAL "Xcode" AND NOT CMAKE_GENERATOR MATCHES "Ninja")
157   add_subdirectory(CMP0021)
158 endif()