File: CheckCXXCompilerHashEmbed.cmake

package info (click to toggle)
nextpnr 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 45,032 kB
  • sloc: cpp: 218,128; python: 24,276; ansic: 10,907; pascal: 1,328; sh: 849; makefile: 563; vhdl: 44; objc: 42; tcl: 41
file content (34 lines) | stat: -rw-r--r-- 1,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
# This is checking for CMAKE_CXX_COMPILER's ability to use `#embed`.
# BBAsm is using `#embed` (C) and not `std::embed` (C++) even though it emits C++ code.
# As of 2025-01-16: Note that the usage of `#embed` in C has different rules (the C++ one is
# an extension, the C one is in the C23 standard).

# Example usage:
#
# check_cxx_compiler_hash_embed(HAS_HASH_EMBED CXX_FLAGS_HASH_EMBED)
# set(CMAKE_CXX_FLAGS ${CXX_FLAGS_HASH_EMBED} ${CMAKE_CXX_FLAGS})
#
function(check_cxx_compiler_hash_embed VAR FLAGS_VAR)
    # Create a binary file with at least one byte that will check for #embed treating things as signed chars
    execute_process(
          COMMAND python3 -c "with open('${CMAKE_CURRENT_BINARY_DIR}/unsigned_bin', 'wb') as f: f.write(b'\\xA5\\x27\\x00')")

    try_compile(
        ${VAR}
        SOURCE_FROM_CONTENT
            compiletest.cc
            "const unsigned char s[] = {\n#embed \"${CMAKE_CURRENT_BINARY_DIR}/unsigned_bin\"\n};\nint main() {}"
    )
    if (${VAR})
        if (FLAGS_VAR)
            check_cxx_compiler_flag(-Wc23-extensions HAS_Wc23-extensions)
            if (HAS_Wc23-extensions)
                set(${FLAGS_VAR} -Wno-c23-extensions PARENT_SCOPE)
            endif()
        endif()

        message(STATUS "C++ compiler supports #embed")
    else()
        message(STATUS "C++ compiler does NOT support #embed")
    endif()
endfunction()