summaryrefslogtreecommitdiff
path: root/Tests/ExportImport/Import/Interface/CMakeLists.txt
blob: ef666b1a77269ac129a5d12500b453c0ff2f5cc5 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

# Import targets from the exported build tree.
include(${Import_BINARY_DIR}/../Export/ExportInterfaceBuildTree.cmake)

# Import targets from the exported install tree.
include(${CMAKE_INSTALL_PREFIX}/lib/exp/expInterface.cmake)

add_library(define_iface INTERFACE)
set_property(TARGET define_iface PROPERTY
  INTERFACE_COMPILE_DEFINITIONS DEFINE_IFACE_DEFINE)

add_executable(headeronlytest_bld headeronlytest.cpp)
target_link_libraries(headeronlytest_bld bld::headeronly)

set_property(TARGET bld::sharediface APPEND PROPERTY INTERFACE_LINK_LIBRARIES define_iface)

add_executable(interfacetest_bld interfacetest.cpp)
target_link_libraries(interfacetest_bld bld::sharediface)

include(CheckCSourceCompiles)
include(CheckCXXSourceCompiles)

macro(do_try_compile prefix)

  set(CMAKE_REQUIRED_LIBRARIES ${prefix}::headeronly)
  check_cxx_source_compiles(
    "
  #include \"headeronly.h\"

  #ifndef HEADERONLY_DEFINE
  #error Expected HEADERONLY_DEFINE
  #endif

  int main(int,char**)
  {
    HeaderOnly ho;
    return ho.foo();
  }
  " ${prefix}IFACE_TRY_COMPILE)

  if(NOT ${prefix}IFACE_TRY_COMPILE)
    message(SEND_ERROR "${prefix} try_compile with IMPORTED INTERFACE target failed!\n\n${OUTPUT}")
  endif()

  if (";${CMAKE_C_COMPILE_FEATURES};" MATCHES ";c_restrict;")
    set(CMAKE_REQUIRED_LIBRARIES ${prefix}::use_c_restrict)
    check_c_source_compiles(
      "
    int foo(int * restrict a, int * restrict b)
    {
      (void)a;
      (void)b;
      return 0;
    }
    int main()
    {
      return 0;
    }
    " ${prefix}IMPORTED_IFACE_C_RESTRICT)

    if(NOT ${prefix}IMPORTED_IFACE_C_RESTRICT)
      message(SEND_ERROR "${prefix} try_compile with IMPORTED INTERFACE target failed!\n\n${OUTPUT}")
    endif()
  endif()
  if (";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";cxx_auto_type;")
    set(CMAKE_REQUIRED_LIBRARIES ${prefix}::use_auto_type)
    check_cxx_source_compiles(
      "
    int main(int,char**)
    {
      auto value = 0;
      return value;
    }
    " ${prefix}IMPORTED_IFACE_AUTO_TYPE)

    if(NOT ${prefix}IMPORTED_IFACE_AUTO_TYPE)
      message(SEND_ERROR "${prefix} try_compile with IMPORTED INTERFACE target failed!\n\n${OUTPUT}")
    endif()
  endif()

endmacro()

do_try_compile(bld)

add_executable(source_target_test_bld source_target_test.cpp)
target_link_libraries(source_target_test_bld bld::source_target)
target_compile_definitions(source_target_test_bld PRIVATE USE_FROM_BUILD_DIR)

add_executable(source_target_test_exp source_target_test.cpp)
target_link_libraries(source_target_test_exp exp::source_target)
target_compile_definitions(source_target_test_exp PRIVATE USE_FROM_INSTALL_DIR)

add_executable(headeronlytest_exp headeronlytest.cpp)
target_link_libraries(headeronlytest_exp exp::headeronly)

set_property(TARGET exp::sharediface APPEND PROPERTY INTERFACE_LINK_LIBRARIES define_iface)

add_executable(interfacetest_exp interfacetest.cpp)
target_link_libraries(interfacetest_exp exp::sharediface)

if(NOT CMAKE_OSX_ARCHITECTURES MATCHES "[;$]" OR CMAKE_GENERATOR STREQUAL "Xcode")
  add_executable(pch_iface_test_bld pch_iface_test.cpp)
  target_link_libraries(pch_iface_test_bld bld::pch_iface)
  add_executable(pch_iface_test_exp pch_iface_test.cpp)
  target_link_libraries(pch_iface_test_exp exp::pch_iface)
  if(CMAKE_CXX_COMPILE_OPTIONS_USE_PCH)
    target_compile_definitions(pch_iface_test_bld PRIVATE EXPECT_PCH)
    target_compile_definitions(pch_iface_test_exp PRIVATE EXPECT_PCH)
  endif()
endif()

do_try_compile(exp)

foreach(ns exp bld)
  get_property(defs TARGET ${ns}::cmakeonly PROPERTY INTERFACE_COMPILE_DEFINITIONS)
  if(NOT defs STREQUAL [[DEF="\"\$\B"]])
    message(SEND_ERROR
      "${ns}::cmakeonly property INTERFACE_COMPILE_DEFINITIONS is:\n"
      " ${defs}\n"
      "not\n"
      " " [[DEF="\"\$\B"]] "\n")
  endif()
  get_property(custom TARGET ${ns}::cmakeonly PROPERTY custom_property)
  if(NOT custom STREQUAL "CustomPropertyValue")
    message(SEND_ERROR
      "${ns}::cmakeonly property custom_property is:\n"
      " ${custom}\n"
      "not\n"
      " CustomPropertyValue\n")
  endif()
endforeach()