1# Copyright (c) 2014      Thomas Heller
2# Copyright (c) 2007-2018 Hartmut Kaiser
3# Copyright (c) 2011      Bryce Lelbach
4#
5# Distributed under the Boost Software License, Version 1.0. (See accompanying
6# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8cmake_policy(PUSH)
9
10hpx_set_cmake_policy(CMP0054 NEW)
11
12function(hpx_setup_target target)
13  # retrieve arguments
14  set(options EXPORT NOHPX_INIT INSTALL NOLIBS PLUGIN NONAMEPREFIX)
15  set(one_value_args TYPE FOLDER NAME SOVERSION VERSION HPX_PREFIX)
16  set(multi_value_args DEPENDENCIES COMPONENT_DEPENDENCIES COMPILE_FLAGS LINK_FLAGS INSTALL_FLAGS)
17  cmake_parse_arguments(target "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
18
19  hpx_is_target(is_target ${target})
20  if(NOT is_target)
21    hpx_error("${target} does not represent a target")
22  endif()
23
24  # Figure out which type we want...
25  if(target_TYPE)
26    string(TOUPPER "${target_TYPE}" _type)
27  else()
28    get_target_property(type_prop ${target} TYPE)
29    if(type_prop STREQUAL "STATIC_LIBRARY")
30      set(_type "LIBRARY")
31    endif()
32    if(type_prop STREQUAL "MODULE_LIBRARY")
33      set(_type "LIBRARY")
34    endif()
35    if(type_prop STREQUAL "SHARED_LIBRARY")
36      set(_type "LIBRARY")
37    endif()
38    if(type_prop STREQUAL "EXECUTABLE")
39      set(_type "EXECUTABLE")
40    endif()
41  endif()
42
43  if(target_FOLDER)
44    set_target_properties(${target} PROPERTIES FOLDER "${target_FOLDER}")
45  endif()
46
47  get_target_property(target_SOURCES ${target} SOURCES)
48
49  # Manage files with .cu extension in case When Cuda Clang is used
50  if(target_SOURCES AND HPX_WITH_CUDA_CLANG)
51    foreach(source ${target_SOURCES})
52      get_filename_component(extension ${source} EXT)
53      if(${extension} STREQUAL ".cu")
54        SET_SOURCE_FILES_PROPERTIES(${source} PROPERTIES
55          COMPILE_FLAGS "${HPX_CUDA_CLANG_FLAGS}")
56      endif()
57    endforeach()
58  endif()
59
60  if(target_COMPILE_FLAGS)
61    hpx_append_property(${target} COMPILE_FLAGS ${target_COMPILE_FLAGS})
62  endif()
63
64  if(target_LINK_FLAGS)
65    hpx_append_property(${target} LINK_FLAGS ${target_LINK_FLAGS})
66  endif()
67
68  if(target_NAME)
69    set(name "${target_NAME}")
70  else()
71    set(name "${target}")
72  endif()
73
74  set(nohpxinit FALSE)
75  if(target_NOHPX_INIT)
76    set(nohpxinit TRUE)
77  endif()
78
79  set(target_STATIC_LINKING OFF)
80  if(HPX_WITH_STATIC_LINKING)
81    set(target_STATIC_LINKING ON)
82  else()
83    set(_hpx_library_type)
84    if(TARGET hpx)
85      get_target_property(_hpx_library_type hpx TYPE)
86    endif()
87
88    if("${_hpx_library_type}" STREQUAL "STATIC_LIBRARY")
89      set(target_STATIC_LINKING ON)
90    endif()
91  endif()
92
93  if(HPX_INCLUDE_DIRS)
94    set_property(TARGET ${target} APPEND
95      PROPERTY INCLUDE_DIRECTORIES
96      "${HPX_INCLUDE_DIRS}"
97    )
98  endif()
99
100  if("${_type}" STREQUAL "EXECUTABLE")
101    if(target_HPX_PREFIX)
102      set(_prefix ${target_HPX_PREFIX})
103    else()
104      set(_prefix ${HPX_PREFIX})
105    endif()
106
107    if(MSVC)
108      string(REPLACE ";" ":" _prefix "${_prefix}")
109    endif()
110
111    set_property(TARGET ${target} APPEND
112                 PROPERTY COMPILE_DEFINITIONS
113                 "HPX_APPLICATION_NAME=${name}"
114                 "HPX_APPLICATION_STRING=\"${name}\""
115                 "HPX_PREFIX=\"${_prefix}\""
116                 "HPX_APPLICATION_EXPORTS")
117  endif()
118
119  if("${_type}" STREQUAL "LIBRARY")
120    set(nohpxinit FALSE)
121    if(DEFINED HPX_LIBRARY_VERSION AND DEFINED HPX_SOVERSION)
122      # set properties of generated shared library
123      set_target_properties(${target}
124        PROPERTIES
125        VERSION ${HPX_LIBRARY_VERSION}
126        SOVERSION ${HPX_SOVERSION})
127    endif()
128    if(NOT target_NONAMEPREFIX)
129      hpx_set_lib_name(${target} ${name})
130    endif()
131    set_target_properties(${target}
132      PROPERTIES
133      # create *nix style library versions + symbolic links
134      # allow creating static and shared libs without conflicts
135      CLEAN_DIRECT_OUTPUT 1
136      OUTPUT_NAME ${name})
137    if(target_PLUGIN)
138      set(plugin_name "HPX_PLUGIN_NAME=hpx_${name}")
139    endif()
140    set(nohpxinit TRUE)
141
142    set_property(TARGET ${target} APPEND
143                 PROPERTY COMPILE_DEFINITIONS
144                 "HPX_LIBRARY_EXPORTS"
145                 ${plugin_name})
146  endif()
147
148  if("${_type}" STREQUAL "COMPONENT")
149    set(nohpxinit FALSE)
150    if(DEFINED HPX_LIBRARY_VERSION AND DEFINED HPX_SOVERSION)
151    # set properties of generated shared library
152      set_target_properties(${target}
153        PROPERTIES
154        VERSION ${HPX_LIBRARY_VERSION}
155        SOVERSION ${HPX_SOVERSION})
156    endif()
157    if(NOT target_NONAMEPREFIX)
158      hpx_set_lib_name(${target} ${name})
159    endif()
160    set_target_properties(${target}
161      PROPERTIES
162      # create *nix style library versions + symbolic links
163      # allow creating static and shared libs without conflicts
164      CLEAN_DIRECT_OUTPUT 1
165      OUTPUT_NAME ${name})
166    set(nohpxinit TRUE)
167
168    set_property(TARGET ${target} APPEND
169                 PROPERTY COMPILE_DEFINITIONS
170                 "HPX_COMPONENT_NAME=hpx_${name}"
171                 "HPX_COMPONENT_STRING=\"hpx_${name}\""
172                 "HPX_COMPONENT_EXPORTS")
173  endif()
174
175  # We force the -DDEBUG and -D_DEBUG defines in debug mode to avoid
176  # ABI differences
177  # if hpx is an imported target, get the config debug/release
178  set(HPX_IMPORT_CONFIG "NOTFOUND")
179  if (TARGET "hpx")
180    get_target_property(HPX_IMPORT_CONFIG "hpx" IMPORTED_CONFIGURATIONS)
181  endif()
182  if(HPX_IMPORT_CONFIG MATCHES NOTFOUND)
183    # we are building HPX not importing, so we should use the $<CONFIG:variable
184    set(_USE_CONFIG 1)
185  else()
186    # hpx is an imported target, so set HPX_DEBUG based on build config of hpx library
187    set(_USE_CONFIG 0)
188  endif()
189
190  if(NOT target_NOLIBS)
191    if(HPX_WITH_DYNAMIC_HPX_MAIN AND ("${_type}" STREQUAL "EXECUTABLE"))
192      if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
193        set(hpx_libs hpx_wrap)
194        set_target_properties(${target} PROPERTIES LINK_FLAGS "-Wl,-wrap=main")
195      elseif(APPLE)
196        set(hpx_libs hpx_wrap)
197        set_target_properties(${target} PROPERTIES LINK_FLAGS "-Wl,-e,_initialize_main")
198      endif()
199    endif()
200    set(hpx_libs ${hpx_libs} hpx)
201    if(NOT target_STATIC_LINKING)
202      set(hpx_libs ${hpx_libs})
203      if(NOT nohpxinit)
204        set(hpx_libs hpx_init ${hpx_libs})
205      endif()
206    endif()
207    hpx_handle_component_dependencies(target_COMPONENT_DEPENDENCIES)
208    set(hpx_libs ${hpx_libs} ${target_COMPONENT_DEPENDENCIES})
209    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
210      set(hpx_libs ${hpx_libs} imf svml irng intlc)
211    endif()
212    if(DEFINED HPX_LIBRARIES)
213      set(hpx_libs ${hpx_libs} ${HPX_LIBRARIES})
214    endif()
215  else()
216    target_compile_options(${target} PUBLIC ${CXX_FLAG})
217  endif()
218
219  target_link_libraries(${target} ${HPX_TLL_PUBLIC} ${hpx_libs} ${target_DEPENDENCIES})
220
221  if(TARGET hpx_internal_flags)
222    target_link_libraries(${target} PRIVATE hpx_internal_flags)
223  endif()
224
225  get_target_property(target_EXCLUDE_FROM_ALL ${target} EXCLUDE_FROM_ALL)
226
227  if(target_EXPORT AND NOT target_EXCLUDE_FROM_ALL)
228    hpx_export_targets(${target})
229    set(install_export EXPORT HPXTargets)
230  endif()
231
232  if(target_INSTALL AND NOT target_EXCLUDE_FROM_ALL)
233    install(TARGETS ${target}
234      ${target_INSTALL_FLAGS}
235      ${install_export}
236    )
237  endif()
238endfunction()
239
240cmake_policy(POP)
241