Skip to content

Instantly share code, notes, and snippets.

@GravisZro
Last active May 15, 2024 12:47
Show Gist options
  • Select an option

  • Save GravisZro/72d57f555bdc23bede6725526377c436 to your computer and use it in GitHub Desktop.

Select an option

Save GravisZro/72d57f555bdc23bede6725526377c436 to your computer and use it in GitHub Desktop.
cmake file for musl
cmake_minimum_required(VERSION 3.18)
project(musl
LANGUAGES C
VERSION 0.0.1)
# set default cmake build type to Debug (None Debug Release RelWithDebInfo MinSizeRel)
if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED ENV{CMAKE_BUILD_TYPE})
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "default build type")
endif()
include(CheckCCompilerFlag)
include(CheckLinkerFlag)
include(CMakePrintHelpers)
find_package(Git QUIET)
# options
option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with ninja)." OFF)
set(ARCH "i386" CACHE STRING "build for x86")
option(OPTIMIZE_BUILD "Make an optimized build" ON)
set(OPTIMIZE_COMPONENTS "internal;malloc;string" CACHE STRING "Which components to optimize")
set(MALLOC_DIR "mallocng" CACHE STRING "Which malloc implementationt to use")
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
# option processing
if(ARCH MATCHES "i386")
message("building for i386")
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
add_compile_options(-target i686-linux-gnu)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
add_compile_options(-m32)
else()
message("compiler id: '${CMAKE_C_COMPILER_ID}'")
endif()
elseif(ARCH MATCHES "x86_64")
message("building for x86_64")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-target x86_64-linux-gnu)
endif()
else()
message("building for ${ARCH}?")
endif()
if(FORCE_COLORED_OUTPUT)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
set(CMAKE_COLOR_DIAGNOSTICS ON)
else()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options (-fdiagnostics-color=always)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options (-fcolor-diagnostics)
endif()
endif()
endif()
set(RVALUE false)
set(CMAKE_REQUIRED_QUIET ON)
function(try_c_flag LISTVAR FLAG)
check_c_compiler_flag(${FLAG} HAS_FLAG)
if(HAS_FLAG)
list(APPEND ${LISTVAR} ${FLAG})
set(${LISTVAR} ${${LISTVAR}} PARENT_SCOPE)
endif()
set(RVALUE ${HAS_FLAG} PARENT_SCOPE)
unset(HAS_FLAG CACHE)
endfunction()
function(try_ld_flag LISTVAR FLAG)
check_linker_flag(C ${FLAG} HAS_FLAG)
if(HAS_FLAG)
list(APPEND ${LISTVAR} ${FLAG})
set(${LISTVAR} ${${LISTVAR}} PARENT_SCOPE)
endif()
set(RVALUE ${HAS_FLAG} PARENT_SCOPE)
unset(HAS_FLAG CACHE)
endfunction()
#
# Figure out options to force errors on unknown flags.
#
try_c_flag(CFLAGS_TRY -Werror=unknown-warning-option)
try_c_flag(CFLAGS_TRY -Werror=unused-command-line-argument)
try_c_flag(CFLAGS_TRY -Werror=ignored-optimization-argument)
try_ld_flag(LDFLAGS_TRY -Werror=unknown-warning-option)
try_ld_flag(LDFLAGS_TRY -Werror=unused-command-line-argument)
#try_c_flag(CFLAGS_C99FSE -std=c99)
try_c_flag(CFLAGS_C99FSE -nostdinc)
try_c_flag(CFLAGS_C99FSE -ffreestanding)
if(NOT RVALUE)
try_c_flag(CFLAGS_C99FSE -fno-builtin)
endif()
try_c_flag(CFLAGS_C99FSE -fexcess-precision=standard)
if(NOT RVALUE AND ARCH MATCHES "i386")
try_c_flag(CFLAGS_C99FSE -ffloat-store)
endif()
try_c_flag(CFLAGS_C99FSE -fno-strict-aliasing)
#
# The GNU toolchain defaults to assuming unmarked files need an
# executable stack, potentially exposing vulnerabilities in programs
# linked with such object files. Fix this.
#
try_c_flag(CFLAGS_C99FSE -Wa,--noexecstack)
#
# Check for options to disable stack protector, which needs to be
# disabled for a few early-bootstrap translation units. If not found,
# this is not an error; we assume the toolchain does not do ssp.
#
try_c_flag(CFLAGS_NOSSP -fno-stack-protector)
#
# Check for options that may be needed to prevent the compiler from
# generating self-referential versions of memcpy, memmove, memcmp,
# and memset. Really, we should add a check to determine if this
# option is sufficient, and if not, add a macro to cripple these
# functions with volatile...
#
try_c_flag(CFLAGS_MEMOPS -fno-tree-loop-distribute-patterns)
#
# Preprocess asm files to add extra debugging information if debug is
# enabled, our assembler supports the needed directives, and the
# preprocessing script has been written for our architecture.
#
#printf "checking whether we should preprocess assembly to add debugging information... "
#if fnmatch '-g*|*\ -g*' "$CFLAGS_AUTO $CFLAGS" &&
# test -f "$srcdir/tools/add-cfi.$ARCH.awk" &&
# printf ".file 1 \"srcfile.s\"\n.line 1\n.cfi_startproc\n.cfi_endproc" | $CC -g -x assembler -c -o /dev/null 2>/dev/null -
#then
# ADD_CFI=yes
#else
# ADD_CFI=no
#fi
#printf "%s\n" "$ADD_CFI"
#
# Possibly add a -O option to CFLAGS and select modules to optimize with
# -O3 based on the status of --enable-optimize and provided CFLAGS.
#
#printf "checking for optimization settings... "
#case "x$optimize" in
#xauto)
#if fnmatch '-O*|*\ -O*' "$CFLAGS_AUTO $CFLAGS" ; then
#printf "using provided CFLAGS\n" ;optimize=no
#else
#printf "using defaults\n" ; optimize=yes
#fi
#;;
#xsize|xnone) printf "minimize size\n" ; optimize=size ;;
#xno|x) printf "disabled\n" ; optimize=no ;;
#*) printf "custom\n" ;;
#esac
if(NOT OPTIMIZE_BUILD)
try_c_flag(CFLAGS_AUTO -O2)
try_c_flag(CFLAGS_AUTO -fno-align-jumps)
try_c_flag(CFLAGS_AUTO -fno-align-functions)
try_c_flag(CFLAGS_AUTO -fno-align-loops)
try_c_flag(CFLAGS_AUTO -fno-align-labels)
try_c_flag(CFLAGS_AUTO -fira-region=one)
try_c_flag(CFLAGS_AUTO -fira-hoist-pressure)
try_c_flag(CFLAGS_AUTO -freorder-blocks-algorithm=simple)
if(NOT RVALUE)
try_c_flag(CFLAGS_AUTO -fno-reorder-blocks)
endif()
try_c_flag(CFLAGS_AUTO -fno-prefetch-loop-arrays)
try_c_flag(CFLAGS_AUTO -fno-tree-ch)
else()
list(FIND OPTIMIZE_COMPONENTS "size" RVALUE)
if(RVALUE EQUAL -1)
message(NOTICE "components to be optimized for speed:")
foreach(COMPONENT ${OPTIMIZE_COMPONENTS})
message(NOTICE " ${COMPONENT}")
file(GLOB "${PROJECT_SOURCE_DIR}/src/${COMPONENT}*.c" GLOBBED_FILES)
list(APPEND OPTIMIZE_GLOBS ${GLOBBED_FILES})
file(GLOB "${PROJECT_SOURCE_DIR}/src/${COMPONENT}/*.c" GLOBBED_FILES)
list(APPEND OPTIMIZE_GLOBS ${GLOBBED_FILES})
endforeach()
endif()
endif()
# Always try -pipe
try_c_flag(CFLAGS_AUTO -pipe)
try_c_flag(CFLAGS_AUTO -B"${PROJECT_BINARY_DIR}")
#try_c_flag(CFLAGS_AUTO -static-libgcc)
file(GLOB SOURCES
src/*/*.c
src/*/${ARCH}/*
src/malloc/${MALLOC_DIR}/*.c
crt/*.c
crt/*/${ARCH}/*
ldso/*.c)
# generate bits/syscall.h
file(READ ${PROJECT_SOURCE_DIR}/arch/${ARCH}/bits/syscall.h.in SYSCALL_H_CONTENT)
file(WRITE ${PROJECT_BINARY_DIR}/include/bits/syscall.h "${SYSCALL_H_CONTENT}")
execute_process(
COMMAND sed -n -e s/__NR_/SYS_/p
INPUT_FILE ${PROJECT_SOURCE_DIR}/arch/${ARCH}/bits/syscall.h.in
OUTPUT_VARIABLE SYSCALL_H_APPENDS)
file(APPEND ${PROJECT_BINARY_DIR}/include/bits/syscall.h ${SYSCALL_H_APPENDS})
# generate bits/alltypes.h
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/include/bits)
execute_process(
COMMAND
sed -f
${PROJECT_SOURCE_DIR}/tools/mkalltypes.sed
${PROJECT_SOURCE_DIR}/arch/${ARCH}/bits/alltypes.h.in
${PROJECT_SOURCE_DIR}/include/alltypes.h.in
OUTPUT_FILE ${PROJECT_BINARY_DIR}/include/bits/alltypes.h
)
# generate version.h
if(GIT_FOUND AND
EXISTS ".git" AND IS_DIRECTORY ".git")
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --match "v[0-9]*"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
string(REGEX REPLACE "v([[:digit:]\.]*)" "\\1" VERSION_STRING "${VERSION_STRING}")
string(REPLACE "-" "-git-" VERSION_STRING "${VERSION_STRING}")
else()
file(READ "VERSION" VERSION_STRING)
string(STRIP ${VERSION_STRING} VERSION_STRING)
endif()
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/src/internal)
file(WRITE ${PROJECT_BINARY_DIR}/include/version.h "#define VERSION \"${VERSION_STRING}\"")
add_library(musl STATIC ${SOURCES})
target_include_directories(musl SYSTEM BEFORE PUBLIC
arch/${ARCH}
arch/generic
src/internal
src/include
${PROJECT_BINARY_DIR}/include
include)
target_compile_definitions(musl PRIVATE _XOPEN_SOURCE=700)
target_compile_options(musl PRIVATE
${CFLAGS_TRY}
${CFLAGS_C99FSE}
${CFLAGS_NOSSP}
${CFLAGS_MEMOPS}
${CFLAGS_AUTO}
)
target_link_options(musl PRIVATE ${LDFLAGS_TRY})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment