xref: /llvm-project/polly/cmake/polly_macros.cmake (revision 2d6d476ffbfc207aae2bf9f12be14483b31d100a)
1macro(add_polly_library name)
2  cmake_parse_arguments(ARG "" "" "" ${ARGN})
3  set(srcs ${ARG_UNPARSED_ARGUMENTS})
4  if(MSVC_IDE OR XCODE)
5    file( GLOB_RECURSE headers *.h *.td *.def)
6    set(srcs ${srcs} ${headers})
7    string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
8    list( GET split_path -1 dir)
9    file( GLOB_RECURSE headers
10      ../../include/polly${dir}/*.h)
11    set(srcs ${srcs} ${headers})
12  endif(MSVC_IDE OR XCODE)
13  if (MODULE)
14    set(libkind MODULE)
15  elseif (SHARED_LIBRARY)
16    set(libkind SHARED)
17  else()
18    set(libkind)
19  endif()
20  add_library( ${name} ${libkind} ${srcs} )
21  set_target_properties(${name} PROPERTIES FOLDER "Polly/Libraries")
22
23  if( LLVM_COMMON_DEPENDS )
24    add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
25  endif( LLVM_COMMON_DEPENDS )
26  if( LLVM_USED_LIBS )
27    foreach(lib ${LLVM_USED_LIBS})
28      target_link_libraries( ${name} PUBLIC ${lib} )
29    endforeach(lib)
30  endif( LLVM_USED_LIBS )
31
32  if(POLLY_LINK_LIBS)
33    foreach(lib ${POLLY_LINK_LIBS})
34      target_link_libraries(${name} PUBLIC ${lib})
35    endforeach(lib)
36  endif(POLLY_LINK_LIBS)
37
38  if( LLVM_LINK_COMPONENTS )
39    llvm_config(${name} ${LLVM_LINK_COMPONENTS})
40  endif( LLVM_LINK_COMPONENTS )
41  if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LLVMPolly")
42    set(exports)
43    if (LLVM_POLLY_LINK_INTO_TOOLS)
44      set(exports EXPORT LLVMExports)
45    endif()
46    install(TARGETS ${name}
47      COMPONENT ${name}
48      ${exports}
49      LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
50      ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
51    add_llvm_install_targets(install-${name}
52      COMPONENT ${name})
53  endif()
54  if (LLVM_POLLY_LINK_INTO_TOOLS)
55    set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
56  endif()
57endmacro(add_polly_library)
58
59macro(add_polly_loadable_module name)
60  set(srcs ${ARGN})
61  # klduge: pass different values for MODULE with multiple targets in same dir
62  # this allows building shared-lib and module in same dir
63  # there must be a cleaner way to achieve this....
64  if (MODULE)
65  else()
66    set(GLOBAL_NOT_MODULE TRUE)
67  endif()
68  set(MODULE TRUE)
69  add_polly_library(${name} ${srcs})
70  set_target_properties(${name} PROPERTIES FOLDER "Polly/Loadable Modules")
71  if (GLOBAL_NOT_MODULE)
72    unset (MODULE)
73  endif()
74  if (APPLE)
75    # Darwin-specific linker flags for loadable modules.
76    set_target_properties(${name} PROPERTIES
77      LINK_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
78  endif()
79endmacro(add_polly_loadable_module)
80
81# Recursive helper for setup_source_group. Traverse the file system and add
82# source files matching the glob_expr to the prefix, recursing into
83# subdirectories as they are encountered
84function(setup_polly_source_groups_helper pwd prefix glob_expr)
85  file(GLOB children RELATIVE ${pwd} ${pwd}/*)
86  foreach(child ${children})
87    if (IS_DIRECTORY ${pwd}/${child})
88      setup_polly_source_groups_helper(${pwd}/${child}
89        "${prefix}\\${child}" ${glob_expr})
90    endif()
91  endforeach()
92
93  file(GLOB to_add ${pwd}/${glob_expr})
94  source_group(${prefix} FILES ${to_add})
95endfunction(setup_polly_source_groups_helper)
96
97# Set up source groups in order to nicely organize source files in IDEs
98macro(setup_polly_source_groups src_root hdr_root)
99  # FIXME: The helper can be eliminated if the CMake version is increased
100  # to 3.8 or higher. If this is done, the TREE version of source_group can
101  # be used
102  setup_polly_source_groups_helper(${src_root} "Source Files" "*.cpp")
103  setup_polly_source_groups_helper(${hdr_root} "Header Files" "*.h")
104endmacro(setup_polly_source_groups)
105