Debian Codegraph @

llvm-project/llvm/projects/opencl-clang/cmake/modules/CMakeFunctions.cmake

raw
1 #
2 # Set compiler RTTI options according to the given flag
3 #
4 macro(use_rtti val)
5 if (MSVC)
6 if( ${val} )
7 llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR-" "/GR")
8 else()
9 llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR" "/GR-" )
10 endif()
11 else () # G++ or clang or icx
12 if( ${val} )
13 llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-fno-rtti" "-frtti")
14 else()
15 llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti" )
16 endif()
17 endif()
18 set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE )
19 endmacro(use_rtti)
20
21 #
22 # Set compiler Exception Handling options according to the given flag
23 #
24 macro(use_eh val)
25 if (MSVC)
26 if( ${val} )
27 llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/EHs-c-" "/EHsc" )
28 add_definitions( /D_HAS_EXCEPTIONS=1 )
29 else()
30 llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/EHsc" "/EHs-c-")
31 add_definitions( /D_HAS_EXCEPTIONS=0 )
32 endif()
33 else () # G++ or clang or icx
34 if( ${val} )
35 remove_definitions( -fno-exceptions )
36 else()
37 add_definitions( -fno-exceptions )
38 endif()
39 endif()
40 endmacro(use_eh)
41
42 # Reads hash-commit from backported patch
43 # This function assumes that each of files starts with (for example):
44 # From 1a400928bf8fc86fa0f062524c25d0985c94ac6f Mon Sep 17 00:00:00 2001
45 function(get_backport_patch_hash patch_path patch_hash)
46 file(READ ${patch_path} first_line LIMIT 40 OFFSET 5)
47 string(STRIP ${first_line} first_line_strip)
48 set(patch_hash ${first_line_strip} PARENT_SCOPE)
49 endfunction()
50
51 # Checks if the the patch is present in current local branch
52 function(is_backport_patch_present patch_path repo_dir patch_in_branch)
53 get_backport_patch_hash(${patch_path} patch_hash)
54 message(STATUS "[OPENCL-CLANG] Checking if patch ${patch_hash} is present in repository")
55 execute_process(
56 COMMAND ${GIT_EXECUTABLE} merge-base --is-ancestor ${patch_hash} HEAD
57 WORKING_DIRECTORY ${repo_dir}
58 RESULT_VARIABLE patch_not_in_branches
59 OUTPUT_QUIET
60 )
61 if(patch_not_in_branches)
62 set(patch_in_branch False PARENT_SCOPE) # The patch is not present in local branch
63 else()
64 set(patch_in_branch True PARENT_SCOPE) # The patch is not present in local branch
65 endif()
66 endfunction()
67
68 # Validates if given SHA1/tag/branch name exists in local repo
69 function(is_valid_revision repo_dir revision return_val)
70 message(STATUS "[OPENCL-CLANG] Validating ${revision} in repository")
71 # Check if we have under revision existing branch/tag/SHA1 in this repo
72 execute_process(
73 COMMAND ${GIT_EXECUTABLE} log -1 ${revision}
74 WORKING_DIRECTORY ${repo_dir}
75 RESULT_VARIABLE output_var
76 OUTPUT_QUIET
77 )
78 if(${output_var} EQUAL 0)
79 set(${return_val} True PARENT_SCOPE) # this tag/branch/sha1 exists in repo
80 else()
81 set(${return_val} False PARENT_SCOPE) # this tag/branch/sha1 not exists in repo
82 endif()
83 endfunction()
84
85 #
86 # Creates `target_branch` starting at the `base_revision` in the `repo_dir`.
87 # Then all patches from the `patches_dir` are committed to the `target_branch`.
88 # Does nothing if the `target_branch` is already checked out in the `repo_dir`.
89 #
90 function(apply_patches repo_dir patches_dir base_revision target_branch)
91 file(GLOB patches ${patches_dir}/*.patch)
92 if(NOT patches)
93 message(STATUS "[OPENCL-CLANG] No patches in ${patches_dir}")
94 return()
95 endif()
96
97 # Check if it's a git repo
98 if(EXISTS "${repo_dir}/.git")
99 message(STATUS "[OPENCL-CLANG] Patching repository ${repo_dir}")
100 else()
101 message(STATUS "[OPENCL-CLANG][Warning] ${repo_dir} is not a git repository, therefore, local patches are not applied")
102 return()
103 endif()
104 # Check if the target branch already exists
105 execute_process(
106 COMMAND ${GIT_EXECUTABLE} rev-parse --verify --no-revs -q ${target_branch}
107 WORKING_DIRECTORY ${repo_dir}
108 RESULT_VARIABLE patches_needed
109 OUTPUT_QUIET
110 )
111 if(patches_needed EQUAL 1) # The target branch doesn't exist
112 list(SORT patches)
113 is_valid_revision(${repo_dir} ${base_revision} exists_base_rev)
114
115 if(NOT ${exists_base_rev})
116 execute_process( # take SHA1 from HEAD
117 COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
118 WORKING_DIRECTORY ${repo_dir}
119 OUTPUT_VARIABLE repo_head
120 OUTPUT_STRIP_TRAILING_WHITESPACE
121 )
122 message(STATUS "[OPENCL-CLANG] ref ${base_revision} not exists in repository, using current HEAD:${repo_head}")
123 set(base_revision ${repo_head})
124 endif()
125 execute_process( # Create the target branch
126 COMMAND ${GIT_EXECUTABLE} checkout -b ${target_branch} ${base_revision}
127 WORKING_DIRECTORY ${repo_dir}
128 RESULT_VARIABLE ret_check_out
129 ERROR_STRIP_TRAILING_WHITESPACE
130 ERROR_VARIABLE checkout_log
131 OUTPUT_QUIET
132 )
133 message(STATUS "[OPENCL-CLANG] ${checkout_log} which starts from ref : ${base_revision}")
134 foreach(patch ${patches})
135 is_backport_patch_present(${patch} ${repo_dir} patch_in_branch)
136 if(${patch_in_branch})
137 message(STATUS "[OPENCL-CLANG] Patch ${patch} is already in local branch - ignore patching")
138 else()
139 execute_process( # Apply the patch
140 COMMAND ${GIT_EXECUTABLE} am --3way --keep-non-patch --ignore-whitespace -C0 ${patch}
141 WORKING_DIRECTORY ${repo_dir}
142 OUTPUT_VARIABLE patching_log
143 )
144 message(STATUS "[OPENCL-CLANG] Not present - ${patching_log}")
145 endif()
146 endforeach(patch)
147 elseif(patches_needed EQUAL 0) # The target branch already exists
148 execute_process( # Check it out
149 COMMAND ${GIT_EXECUTABLE} checkout ${target_branch}
150 WORKING_DIRECTORY ${repo_dir}
151 OUTPUT_QUIET
152 )
153 endif()
154 if (NOT (ret_check_out OR ret_apply_patch))
155 message(STATUS "[OPENCL-CLANG] Applied patch successfully!")
156 else()
157 message(FATAL_ERROR "[OPENCL-CLANG] Failed to apply patch!")
158 endif()
159 endfunction()
160
161 # Usage
162 # SET_LINUX_EXPORTS_FILE( TARGET FILE_NAME )
163 # This function provides special linkage options for OCL Back-End. These options
164 # prevents exposure of function symbols externally (except functions which are
165 # explicitly listed in version script)
166 function ( SET_LINUX_EXPORTS_FILE TARGET FILE_NAME )
167 if(NOT WIN32)
168 get_target_property( SOURCE_FILES ${TARGET_NAME} SOURCES )
169 list( GET SOURCE_FILES 0 FIRST_SOURCE )
170 set_source_files_properties( ${FIRST_SOURCE} PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE_NAME} )
171 set_property( TARGET ${TARGET_NAME} APPEND_STRING PROPERTY
172 LINK_FLAGS " -Wl,-Bsymbolic -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/${FILE_NAME}" )
173 endif(NOT WIN32)
174 endfunction ( SET_LINUX_EXPORTS_FILE )