Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add detection for custom Linker #232

Merged
merged 10 commits into from
Aug 8, 2023
9 changes: 8 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,11 @@ services:
build:
context: .
dockerfile: ./docker/Dockerfile.arm-bare-metal
target: build
target: build
build-gcc-with-custom-linker:
build:
context: .
dockerfile: ./docker/Dockerfile
args:
compiler: gcc
target: build-with-custom-linker
14 changes: 14 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ RUN setup-cpp --compiler $compiler --llvm true --cmake true --doxygen true --nin
COPY ./docker/entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT [ "/docker-entrypoint.sh" ]

FROM setup AS setup-custom-linker

# install linker
RUN apt-get update && apt-get install -y \
binutils \
mold \
lld \
&& rm -rf /var/lib/apt/lists/*

FROM setup AS build
COPY . /home/project_options
Expand All @@ -30,3 +38,9 @@ WORKDIR /home/project_options
RUN git submodule update --init
CMD ["/bin/bash", "-c", "task myproj:test"]


FROM setup-custom-linker AS build-with-custom-linker
COPY . /home/project_options
WORKDIR /home/project_options
RUN git submodule update --init
CMD ["/bin/bash", "-c", "task myproj:build"]
4 changes: 4 additions & 0 deletions docker/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ tasks:
- docker-compose up --build test-gcc
- docker-compose down

gcc.custom-linker:
- docker-compose up --build build-gcc-with-custom-linker
- docker-compose down

llvm:
- docker-compose up --build build-llvm
- docker-compose up --build test-llvm
Expand Down
21 changes: 19 additions & 2 deletions src/Linker.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
include_guard()

# Test linker option support
function(test_linker_option output_linker_test linker)
set(output_linker_test OFF PARENT_SCOPE)
if(NOT "${linker}" STREQUAL "")
include(CheckCXXCompilerFlag)

set(_linker_flag "-fuse-ld=${linker}")

check_cxx_compiler_flag(${_linker_flag} _cxx_supports_linker)
if("${_cxx_supports_linker}" STREQUAL "1")
set(${output_linker_test} ON PARENT_SCOPE)
else()
set(${output_linker_test} OFF PARENT_SCOPE)
endif()
endif()
endfunction()

# Set the linker to use for the linking phase
macro(configure_linker _project_name _linker)
if(NOT "${_linker}" STREQUAL "")
Expand All @@ -8,8 +25,8 @@ macro(configure_linker _project_name _linker)

set(_linker_flag "-fuse-ld=${_linker}")

check_cxx_compiler_flag(${_linker_flag} _cxx_supports_linker)
if("${_cxx_supports_linker}" STREQUAL "1")
test_linker_option(_cxx_supports_linker ${_linker})
if(_cxx_supports_linker)
message(TRACE "Using ${_linker} as the linker for ${_project_name}")
target_link_options(${_project_name} INTERFACE ${_linker_flag})
else()
Expand Down
33 changes: 33 additions & 0 deletions src/Utilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,36 @@ function(detect_architecture arch)
set(${arch} x64 PARENT_SCOPE)
endif()
endfunction()

# detect custom linker
function(detect_custom_linker LINKER)
set(SUPPORTS_LLD OFF)
set(SUPPORTS_GOLD OFF)
set(SUPPORTS_MOLD OFF)
if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*") AND NOT WIN32)
aminya marked this conversation as resolved.
Show resolved Hide resolved
find_program(PROGRAM_LLD NAMES "lld")
if(EXISTS ${PROGRAM_LLD})
test_linker_option(SUPPORTS_LLD "lld")
endif()
endif()
if((CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND NOT WIN32)
find_program(PROGRAM_GOLD NAMES "gold")
if(EXISTS ${PROGRAM_GOLD})
test_linker_option(SUPPORTS_GOLD "gold")
endif()
endif()
if(UNIX AND NOT WIN32 AND NOT APPLE)
aminya marked this conversation as resolved.
Show resolved Hide resolved
find_program(PROGRAM_MOLD NAMES "mold")
if(EXISTS ${PROGRAM_MOLD})
test_linker_option(SUPPORTS_MOLD "mold")
endif()
endif()

if(SUPPORTS_MOLD)
set(${LINKER} "mold" PARENT_SCOPE)
elseif(SUPPORTS_LLD)
set(${LINKER} "lld" PARENT_SCOPE)
elseif(SUPPORTS_GOLD)
set(${LINKER} "gold" PARENT_SCOPE)
endif()
endfunction()
7 changes: 7 additions & 0 deletions tests/myproj/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ else()
endif()
endif()

# Detect custom linker
if(NOT MSVC)
detect_custom_linker(LINKER)
#message(STATUS "Detect Linker: ${LINKER}")
endif()

# Initialize project_options
# uncomment the options to enable them
project_options(
Expand Down Expand Up @@ -73,6 +79,7 @@ project_options(
# ENABLE_SANITIZER_THREAD
# ENABLE_SANITIZER_MEMORY
# CLANG_WARNINGS "-Weverything"
LINKER "${LINKER}"
)
# NOTE: project_options and project_warnings are defined inside project_options

Expand Down
Loading