File: CMakeFunctions.cmake

package info (click to toggle)
opencl-clang-15 15.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 204 kB
  • sloc: cpp: 1,001; makefile: 18; ansic: 5
file content (178 lines) | stat: -rw-r--r-- 7,385 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#
# Set compiler RTTI options according to the given flag
#
macro(use_rtti val)
    if( CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
        if( ${val} )
            llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-fno-rtti" "-frtti")
        else()
            llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti" )
        endif()
    else(MSVC)
        if( ${val} )
            llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR-" "/GR")
        else()
            llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR" "/GR-" )
        endif()
    endif()
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE )
endmacro(use_rtti)

#
# Set compiler Exception Handling options according to the given flag
#
macro(use_eh val)
    if( CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
        if( ${val} )
            remove_definitions( -fno-exceptions )
        else()
            add_definitions( -fno-exceptions )
        endif()
    else(MSVC)
        if( ${val} )
              llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/EHs-c-" "/EHsc" )
              add_definitions( /D_HAS_EXCEPTIONS=1 )
        else()
              llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/EHsc" "/EHs-c-")
              add_definitions( /D_HAS_EXCEPTIONS=0 )
        endif()
    endif()
endmacro(use_eh)

# Reads hash-commit from backported patch
# This function assumes that each of files starts with (for example):
# From 1a400928bf8fc86fa0f062524c25d0985c94ac6f Mon Sep 17 00:00:00 2001
function(get_backport_patch_hash patch_path patch_hash)
    file(READ ${patch_path} first_line LIMIT 40 OFFSET 5)
    string(STRIP ${first_line} first_line_strip)
    set(patch_hash ${first_line_strip} PARENT_SCOPE)
endfunction()

# Checks if the the patch is present in current local branch
function(is_backport_patch_present patch_path repo_dir patch_in_branch)
    get_backport_patch_hash(${patch_path} patch_hash)
    message(STATUS "[OPENCL-CLANG] Checking if patch ${patch_hash} is present in repository")
    execute_process(
        COMMAND ${GIT_EXECUTABLE} merge-base --is-ancestor ${patch_hash} HEAD
        WORKING_DIRECTORY ${repo_dir}
        RESULT_VARIABLE patch_not_in_branches
        OUTPUT_QUIET
        )
    if(patch_not_in_branches)
        set(patch_in_branch False PARENT_SCOPE) # The patch is not present in local branch
    else()
        set(patch_in_branch True PARENT_SCOPE)  # The patch is not present in local branch
    endif()
endfunction()

# Validates if given SHA1/tag/branch name exists in local repo
function(is_valid_revision repo_dir revision return_val)
    message(STATUS "[OPENCL-CLANG] Validating ${revision} in repository")
    # Check if we have under revision existing branch/tag/SHA1 in this repo
    execute_process(
        COMMAND ${GIT_EXECUTABLE} log -1 ${revision}
        WORKING_DIRECTORY ${repo_dir}
        RESULT_VARIABLE output_var
        OUTPUT_QUIET
        )
    if(${output_var} EQUAL 0)
        set(${return_val} True PARENT_SCOPE) # this tag/branch/sha1 exists in repo
    else()
        set(${return_val} False PARENT_SCOPE) # this tag/branch/sha1 not exists in repo
    endif()
endfunction()

#
# Creates `target_branch` starting at the `base_revision` in the `repo_dir`.
# Then all patches from the `patches_dir` are committed to the `target_branch`.
# Does nothing if the `target_branch` is already checked out in the `repo_dir`.
#
function(apply_patches repo_dir patches_dir base_revision target_branch ret)
    foreach(patches_dir ${patches_dir})
        file(GLOB patches_in_dir ${patches_dir}/*.patch)
        list(APPEND patches ${patches_in_dir})
    endforeach()
    if(NOT patches)
        message(STATUS "[OPENCL-CLANG] No patches in ${patches_dir}")
        return()
    endif()

    message(STATUS "[OPENCL-CLANG] Patching repository ${repo_dir}")
    # Check if the target branch already exists
    execute_process(
        COMMAND ${GIT_EXECUTABLE} rev-parse --verify --no-revs -q ${target_branch}
        WORKING_DIRECTORY ${repo_dir}
        RESULT_VARIABLE patches_needed
        OUTPUT_QUIET
    )
    if(patches_needed EQUAL 128) # not a git repo
        set(ret_not_git_repo 1)
	elseif(patches_needed) # The target branch doesn't exist
        list(SORT patches)
        is_valid_revision(${repo_dir} ${base_revision} exists_base_rev)

        if(NOT ${exists_base_rev})
            execute_process( # take SHA1 from HEAD
                COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
                WORKING_DIRECTORY ${repo_dir}
                OUTPUT_VARIABLE repo_head
                OUTPUT_STRIP_TRAILING_WHITESPACE
                )
            message(STATUS "[OPENCL-CLANG] ref ${base_revision} not exists in repository, using current HEAD:${repo_head}")
            set(base_revision ${repo_head})
        endif()
        execute_process( # Create the target branch
            COMMAND ${GIT_EXECUTABLE} checkout -b ${target_branch} ${base_revision}
            WORKING_DIRECTORY ${repo_dir}
            RESULT_VARIABLE ret_check_out
            ERROR_STRIP_TRAILING_WHITESPACE
            ERROR_VARIABLE checkout_log
            OUTPUT_QUIET
            )
        message(STATUS "[OPENCL-CLANG] ${checkout_log} which starts from ref : ${base_revision}")
        foreach(patch ${patches})
            is_backport_patch_present(${patch} ${repo_dir} patch_in_branch)
            if(${patch_in_branch})
                message(STATUS "[OPENCL-CLANG] Patch ${patch} is already in local branch - ignore patching")
            else()
                execute_process( # Apply the patch
                    COMMAND ${GIT_EXECUTABLE} am --3way --ignore-whitespace -C0 ${patch}
                    WORKING_DIRECTORY ${repo_dir}
                    OUTPUT_VARIABLE patching_log
                    RESULT_VARIABLE ret_apply_patch
                )
                message(STATUS "[OPENCL-CLANG] Not present - ${patching_log}")
                if (ret_apply_patch)
                    break()
                endif()
            endif()
        endforeach(patch)
    else() # The target branch already exists
        execute_process( # Check it out
            COMMAND ${GIT_EXECUTABLE} checkout ${target_branch}
            WORKING_DIRECTORY ${repo_dir}
            OUTPUT_QUIET
            RESULT_VARIABLE ret_check_out
        )
    endif()
	if (NOT (ret_not_git_repo OR ret_check_out OR ret_apply_patch))
        set(${ret} True PARENT_SCOPE)
    else()
        message(FATAL_ERROR "[OPENCL-CLANG] Failed to apply patch!")
    endif()
endfunction()

# Usage
#     SET_LINUX_EXPORTS_FILE( TARGET FILE_NAME )
# This function provides special linkage options for OCL Back-End. These options
# prevents exposure of function symbols externally (except functions which are
# explicitly listed in version script)
function ( SET_LINUX_EXPORTS_FILE TARGET FILE_NAME )
    if(NOT WIN32)
        get_target_property( SOURCE_FILES ${TARGET_NAME} SOURCES )
        list( GET SOURCE_FILES 0 FIRST_SOURCE )
        set_source_files_properties( ${FIRST_SOURCE} PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE_NAME} )
        set_property( TARGET ${TARGET_NAME} APPEND_STRING PROPERTY
                        LINK_FLAGS " -Wl,-Bsymbolic -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/${FILE_NAME}" )
    endif(NOT WIN32)
endfunction ( SET_LINUX_EXPORTS_FILE )