project(musl C ASM ) cmake_minimum_required(VERSION 3.0) if(NOT DEFINED MUSL_ARCH) set(MUSL_ARCH ${CMAKE_SYSTEM_PROCESSOR} ) endif() if(NOT DEFINED MUSL_SYSLIBDIR) set(MUSL_SYSLIBDIR "$ENV{DESTDIR}/lib" ) endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \ -fPIC \ -nodefaultlibs \ -fexceptions\ -Wall \ -std=c99 \ -isysroot${CMAKE_SOURCE_DIR}/include \ -D_XOPEN_SOURCE=700\ -Os\ -pipe\ -ffreestanding" ) set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1) include_directories( "${CMAKE_SOURCE_DIR}/arch/generic/" "${CMAKE_SOURCE_DIR}/arch/${MUSL_ARCH}" "${CMAKE_BINARY_DIR}" "${CMAKE_BINARY_DIR}/arch" ) file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/arch/bits/") add_custom_target(alltypes COMMAND sed -f "${CMAKE_SOURCE_DIR}/tools/mkalltypes.sed" "${CMAKE_SOURCE_DIR}/arch/${MUSL_ARCH}/bits/alltypes.h.in" "${CMAKE_SOURCE_DIR}/include/alltypes.h.in" > "${CMAKE_CURRENT_BINARY_DIR}/arch/bits/alltypes.h" ) add_custom_target(syscall COMMAND sed -n -e s/__NR_/SYS_/p < "${CMAKE_SOURCE_DIR}/arch/${MUSL_ARCH}/bits/syscall.h.in" > "${CMAKE_CURRENT_BINARY_DIR}/arch/bits/syscall.h" ) execute_process( COMMAND sh tools/version.sh COMMAND_ECHO STDOUT RESULT_VARIABLE version WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} ) file(WRITE "${CMAKE_BINARY_DIR}/version.h" "#define VERSION \"${version}\"") if(EXISTS "${CMAKE_SOURCE_DIR}/src/thread/${MUSL_ARCH}") list(APPEND muslSrcs "${CMAKE_SOURCE_DIR}/src/thread/${MUSL_ARCH}/syscall_cp.s" ) file(GLOB thread_arch_src "${CMAKE_SOURCE_DIR}/src/thread/${MUSL_ARCH}/*.s" ) endif() file(GLOB crtSrcs "${CMAKE_SOURCE_DIR}/crt/*.c" ) file(GLOB crtArchSrcs "${CMAKE_SOURCE_DIR}/crt/${MUSL_ARCH}/*.s" ) file( GLOB muslSrcs "${CMAKE_SOURCE_DIR}/ldso/*.c" "${CMAKE_SOURCE_DIR}/src/*/*.c" "${CMAKE_SOURCE_DIR}/src/malloc/mallocng/*.c" ) file(GLOB muslArchSrcs "${CMAKE_SOURCE_DIR}/src/*/${MUSL_ARCH}/*.c" "${CMAKE_SOURCE_DIR}/src/*/${MUSL_ARCH}/*.s" ) list(FILTER muslSrcs EXCLUDE REGEX "${CMAKE_SOURCE_DIR}/src/malloc/oldmalloc*") macro(replace_list_item LIST OLD_VALUE NEW_VALUE) list(FIND ${LIST} ${OLD_VALUE} OLD_VALUE_INDEX) if(OLD_VALUE_INDEX GREATER_EQUAL 0) list(REMOVE_AT ${LIST} ${OLD_VALUE_INDEX}) list(INSERT ${LIST} ${OLD_VALUE_INDEX} ${NEW_VALUE}) endif() endmacro() foreach(srcf ${muslSrcs}) get_filename_component(srcfname ${srcf} NAME_WLE) foreach(acsrcf ${muslArchSrcs}) get_filename_component(acsrcfname ${acsrcf} NAME_WLE) if(${srcfname} STREQUAL ${acsrcfname}) replace_list_item(muslSrcs ${srcf} ${acsrcf}) endif() endforeach() endforeach() file (GLOB arch_mathsrc "${CMAKE_SOURCE_DIR}/src/math/${MUSL_ARCH}/*.c" "${CMAKE_SOURCE_DIR}/src/math/${MUSL_ARCH}/*.s" ) file (GLOB mathsrc "${CMAKE_SOURCE_DIR}/src/math/*.c" ) foreach(arfp ${arch_mathsrc}) get_filename_component(arfname ${arfp} NAME_WLE) foreach(fp ${mathsrc}) get_filename_component(fpfname ${fp} NAME_WLE) if(${fpfname} STREQUAL ${arfname}) replace_list_item(mathsrc ${fp} ${arfp}) endif() endforeach() endforeach() add_library( crt OBJECT ${crtSrcs} ) target_include_directories( crt PRIVATE "${CMAKE_SOURCE_DIR}/src/internal" "${CMAKE_SOURCE_DIR}/src/include" "${CMAKE_SOURCE_DIR}/include" "${CMAKE_SOURCE_DIR}/src/internal/dynlink.h" "${CMAKE_SOURCE_DIR}/arch/${MUSL_ARCH}/reloc.h" "${CMAKE_SOURCE_DIR}/arch/${MUSL_ARCH}/crt_arch.h" ) target_compile_definitions(crt PRIVATE "-DCRT" ) list(APPEND NOSSP_OBJS ${muslSrcs}) list(FILTER NOSSP_OBJS EXCLUDE REGEX "/*/__libc_start_main.c|/*/__init_tls.c|/*/__stack_chk_fail.c|/*/__set_thread_area.c|/*/memset.c|/*/memcpy.c") add_library(muslobj OBJECT ${muslSrcs} ) add_dependencies(muslobj syscall alltypes) target_include_directories(muslobj PRIVATE "${CMAKE_SOURCE_DIR}/src/include" "${CMAKE_SOURCE_DIR}/src/internal" "${CMAKE_SOURCE_DIR}/src/internal/stdio_impl.h" "${CMAKE_SOURCE_DIR}/src/internal/pthread_impl.h" "${CMAKE_SOURCE_DIR}/src/internal/locale_impl.h" "${CMAKE_SOURCE_DIR}/src/internal/libc.h" "${CMAKE_BINARY_DIR}/arch" PUBLIC "${CMAKE_SOURCE_DIR}/include" ) #shared build add_library(musl SHARED $ ) SET_SOURCE_FILES_PROPERTIES( "${CMAKE_SOURCE_DIR}/crt/crt1.c" PROPERTIES GENERATED TRUE ) target_include_directories(musl PUBLIC "${CMAKE_SOURCE_DIR}/include" ) target_link_options(musl PUBLIC "-Wl,-e,_dlstart" ) target_link_libraries(musl PRIVATE "gcc" ) set_target_properties(musl PROPERTIES OUTPUT_NAME c) add_dependencies(musl muslobj) #statiuc build add_library(musl-static STATIC $ ) target_include_directories(musl-static PRIVATE "${CMAKE_SOURCE_DIR}/src/include" "${CMAKE_SOURCE_DIR}/src/internal" PUBLIC "${CMAKE_SOURCE_DIR}/include" ) set_target_properties(musl-static PROPERTIES OUTPUT_NAME c) add_dependencies(musl-static muslobj) target_link_libraries(musl PRIVATE "gcc" ) file(CREATE_LINK ${MUSL_SYSLIBDIR}/libc.so ${CMAKE_BINARY_DIR}/ld-musl-${MUSL_ARCH}.so.1 SYMBOLIC) add_dependencies(musl-static syscall alltypes) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/libm.so SYMBOLIC) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/librt.so SYMBOLIC) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/libpthread.so SYMBOLIC) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/libcrypt.so SYMBOLIC) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/libutil.so SYMBOLIC) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/libxnet.so SYMBOLIC) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/libresolv.so SYMBOLIC) file(CREATE_LINK libc.so ${CMAKE_BINARY_DIR}/libdl.so SYMBOLIC) install( FILES "${CMAKE_BINARY_DIR}/libm.so" "${CMAKE_BINARY_DIR}/librt.so" "${CMAKE_BINARY_DIR}/libpthread.so" "${CMAKE_BINARY_DIR}/libcrypt.so" "${CMAKE_BINARY_DIR}/libutil.so" "${CMAKE_BINARY_DIR}/libxnet.so" "${CMAKE_BINARY_DIR}/libresolv.so" "${CMAKE_BINARY_DIR}/libdl.so" DESTINATION lib COMPONENT lib ) install( TARGETS musl musl-static DESTINATION lib ) install( FILES ${CMAKE_BINARY_DIR}/ld-musl-${MUSL_ARCH}.so.1 DESTINATION ${MUSL_SYSLIBDIR} ) install( FILES "${CMAKE_BINARY_DIR}/arch/bits/syscall.h" "${CMAKE_BINARY_DIR}/arch/bits/alltypes.h" DESTINATION include/bits ) install( DIRECTORY ${CMAKE_SOURCE_DIR}/include/ ${CMAKE_SOURCE_DIR}/arch/generic/ ${CMAKE_SOURCE_DIR}/arch/${MUSL_ARCH}/ DESTINATION include ) install( FILES $ DESTINATION lib )