Skip to content

Commit

Permalink
Support for static executable (#101)(#163)
Browse files Browse the repository at this point in the history
* Build script: --static flag
* Static compile flags in CMakeLists
  • Loading branch information
bricoletc committed Jan 25, 2022
1 parent 83d682b commit ef92c63
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
49 changes: 39 additions & 10 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
#!/usr/bin/env bash
set -eu -o pipefail

TARGETS="gram|all|test_main"
BUILD_TYPES="DEBUG|REL_WITH_ASSERTS"

usage(){
echo "Builds the gramtools backend"
echo "usage: $0 [build_target] [build_mode]"
echo "\tbuild_target: $TARGETS (default: gram)"
echo "\tbuild_mode: $BUILD_TYPES (default: REL_WITH_ASSERTS)"
echo "usage: $0 [--target ($TARGETS)] [--build_type ($BUILD_TYPES)] [--static]"
echo -e "\tDefaults: --target gram --build_type REL_WITH_ASSERTS"
echo -e "\tPass --static to make a standalone executable"
exit 0
}

TARGETS="gram|all|test_main"
BUILD_TYPES="DEBUG|REL_WITH_ASSERTS"
TARGET=${1:-gram}
BUILD_TYPE=${2:-REL_WITH_ASSERTS}
[[ "$BUILD_TYPE" =~ $BUILD_TYPES ]] || usage
[[ "$TARGET" =~ $TARGETS ]] || usage
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--target)
TARGET="$2"
[[ "$TARGET" =~ $TARGETS ]] || usage
shift
shift
;;
--build_type)
BUILD_TYPE="$2"
[[ "$BUILD_TYPE" =~ $BUILD_TYPES ]] || usage
shift
shift
;;
--static)
STATIC="-static"
shift
;;
*) # unknown option
usage
;;
esac
done

TARGET=${TARGET:-gram}
BUILD_TYPE=${BUILD_TYPE:-REL_WITH_ASSERTS}
STATIC=${STATIC:- }
[[ "$STATIC" =~ -static| ]] || usage

BASE_DIR=$(realpath $(dirname $0))
CUR_DIR=$(pwd)
Expand All @@ -33,8 +60,10 @@ echo "Building in: ${BUILD_DIR}" >&1
echo "Writing stdout to: ${STDOUT_FILE}" >&1
mkdir -p "${BUILD_DIR}" && cd "${BUILD_DIR}"

CMAKE_OPTS="-DCMAKE_EXE_LINKER_FLAGS=${STATIC} -DCMAKE_BUILD_TYPE=${BUILD_TYPE}"

conan install .. -s compiler.libcxx=libstdc++11 --build=missing > "$STDOUT_FILE"
CC=gcc CXX=g++ cmake -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" .. >> "$STDOUT_FILE"
CC=gcc CXX=g++ cmake "$CMAKE_OPTS" .. | tee -a "$STDOUT_FILE"
make -j 4 "${TARGET}" | tee -a "$STDOUT_FILE"

cd "$CUR_DIR"
38 changes: 23 additions & 15 deletions libgramtools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ add_custom_target(py_git_version
COMMAND make -C ${PROJECT_SOURCE_DIR}/gramtools/version)


######################
### Compile flags ##
######################
### Build type flags ##
# add a RELEASE_WITH_ASSERTS build type. Use via `cmake -DCMAKE_BUILD_TYPE=REL_WITH_ASSERTS`
# Note: this variable name NEEDS TO BE UPPERCASE (but not in cmake call)
set(CMAKE_CXX_FLAGS_REL_WITH_ASSERTS "-O3")
# Note: this variable name needs to be uppercased here
set(CMAKE_CXX_FLAGS_REL_WITH_ASSERTS "${CMAKE_CXX_FLAGS_REL_WITH_ASSERTS} -O3")


######################
Expand All @@ -23,7 +21,14 @@ include(${PROJECT_SOURCE_DIR}/libgramtools/lib/htslib.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(OpenMP REQUIRED)
# Parallelisation
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(MACOSX TRUE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNO_OPENMP")
else ()
find_package(OpenMP REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
endif()

### Include headers ####
set(EXTERNAL_INCLUDE_DIR
Expand All @@ -33,9 +38,10 @@ set(EXTERNAL_INCLUDE_DIR
#### Link libraries ####
link_directories(${CMAKE_CURRENT_BINARY_DIR}/lib)

set(EXTERN_LIBS
divsufsort
divsufsort64
set(SDSL_LIBS
-ldivsufsort
-ldivsufsort64
-lsdsl
)

#########################
Expand All @@ -54,27 +60,29 @@ file(GLOB_RECURSE HEADER_FILES
${INCLUDE}/*.hpp)



#########################
#### libgramtools ####
#########################
if (${CMAKE_EXE_LINKER_FLAGS} MATCHES "-static")
set(STATIC_FLAGS "-static-libgcc -static-libstdc++ -ldl")
endif()
add_library(gramtools STATIC
${SOURCE_FILES}
${HEADER_FILES} # For IDE to recognise header files as part of a target
)

target_include_directories(gramtools PUBLIC
${INCLUDE}
${EXTERNAL_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/libgramtools/lib
)
target_link_libraries(gramtools LINK_PUBLIC
${EXTERN_LIBS}
${CMAKE_CURRENT_BINARY_DIR}/lib/libsdsl.a
${SDSL_LIBS}
${CMAKE_CURRENT_BINARY_DIR}/lib/libhts.a
CONAN_PKG::boost
CONAN_PKG::nlohmann_json
OpenMP::OpenMP_CXX
-lstdc++fs -lpthread -lrt -lm -lz
${STATIC_FLAGS}
)
target_compile_features(gramtools PUBLIC cxx_std_17)
set_target_properties(gramtools
Expand Down Expand Up @@ -134,8 +142,8 @@ if (EXISTS "${PROJECT_SOURCE_DIR}/libgramtools/tests")
"${PROJECT_SOURCE_DIR}/libgramtools/tests/*"
)
include(CodeCoverage)
set(CMAKE_CXX_FLAGS "-O0")
set(CMAKE_C_FLAGS "-O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
append_coverage_compiler_flags()
setup_target_for_coverage_lcov(
NAME test_coverage
Expand Down
2 changes: 1 addition & 1 deletion libgramtools/lib/htslib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ExternalProject_Add(htslib
DOWNLOAD_COMMAND wget https://github.com/samtools/htslib/releases/download/1.10/htslib-1.10.tar.bz2 --timestamping
DOWNLOAD_DIR "${CMAKE_CURRENT_BINARY_DIR}/download"
CONFIGURE_COMMAND bash -c "cd ${CMAKE_CURRENT_BINARY_DIR}/src/htslib-1.10 && \
autoheader && autoconf && ./configure --prefix=${CMAKE_CURRENT_BINARY_DIR} \
./configure --prefix=${CMAKE_CURRENT_BINARY_DIR} \
--disable-bz2 --disable-lzma --enable-libcurl=no"
BUILD_COMMAND bash -c "cd ${CMAKE_CURRENT_BINARY_DIR}/src/htslib-1.10 && make && make install"
INSTALL_COMMAND ""
Expand Down

0 comments on commit ef92c63

Please sign in to comment.