Skip to content

Commit

Permalink
rapidyaml static linking
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Jan 2, 2025
1 parent c7375b8 commit f08cc57
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rapidyaml/native/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/rapidyaml/
/rapidyaml-build/
/_build/
/rapidyaml_all.hpp
/librapidyaml.*
68 changes: 56 additions & 12 deletions rapidyaml/native/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,65 @@ include $(COMMON)/java.mk
include $(COMMON)/python.mk
include ../rapidyaml.mk

# TODO change to static library!
# https://www.graalvm.org/latest/reference-manual/native-image/guides/build-static-executables/
# https://www.blog.akhil.cc/static-jni
# https://stackoverflow.com/questions/24493337/linking-static-library-with-jni
SHARED := 1
# when SHARED=0, also link statically with the musl libc
MUSL := 1
# enable timing scopes
TIMED := 0

RAPIDYAML_H := org_rapidyaml_Rapidyaml.h
RAPIDYAML_HPP := rapidyaml_all.hpp
RAPIDYAML_AMALGAMATE_OPTS := --stl

RAPIDYAML_SO_DEPS := \
ifeq ($(SHARED),0)
RAPIDYAML_LIB := librapidyaml.a
else
RAPIDYAML_LIB := librapidyaml.$(RAPIDYAML_VERSION).so
endif

RAPIDYAML_LIB_DEPS := \
Makefile \
CMakeLists.txt \
$(JAVA_HOME) \
$(RAPIDYAML_H) \
$(RAPIDYAML_HPP) \
$(wildcard ./*pp) \

# TODO maybe change to static library!
# https://stackoverflow.com/questions/24493337/linking-static-library-with-jni
SHARED := 1
TIMED := 0
# fixme download and unpack https://musl.cc/x86_64-linux-musl-cross.tgz
THIS_DIR := $(shell pwd)
MUSL_DIR := $(THIS_DIR)/_build/x86_64-linux-musl-cross

CMK_ENV :=
CMK_FLAGS :=
CMK_FLAGS += -D YS2EDN_TIMED=$(TIMED)
CMK_FLAGS += -D BUILD_SHARED_LIBS=$(SHARED)

BDIR := rapidyaml-build
ifeq ($(SHARED),0)
ifneq ($(MUSL),0)
CMK_ENV += MUSL_DIR=$(MUSL_DIR)
CMK_FLAGS += \
-D CMAKE_TOOLCHAIN_FILE=musl.x86_64.cmake \
-D CMAKE_C_FLAGS='-static' \
-D CMAKE_CXX_FLAGS='-static'
endif
endif

BDIR := $(THIS_DIR)/_build/shared$(SHARED)-musl$(MUSL)-timed$(TIMED)

CMK_BUILD := cmake --build $(BDIR) --parallel --verbose


#------------------------------------------------------------------------------
default::

build:: $(RAPIDYAML_SO)
.PHONY: build
build:: $(RAPIDYAML_LIB)

test:: build
$(CMK_BUILD) --target rapidyaml-test-run
./$(BDIR)/rapidyaml-test

rapidyaml:
mkdir -p $@
Expand All @@ -58,11 +83,30 @@ realclean:: clean


#------------------------------------------------------------------------------
$(RAPIDYAML_SO): $(RAPIDYAML_SO_DEPS)
cmake -S . -B $(BDIR) $(CMK_FLAGS)
$(BDIR)/CMakeCache.txt:
mkdir -p $(BDIR)
$(CMK_ENV) cmake -S . -B $(BDIR) $(CMK_FLAGS)

$(RAPIDYAML_LIB): $(RAPIDYAML_LIB_DEPS) $(BDIR)/CMakeCache.txt
$(CMK_BUILD) --target rapidyaml
mv $(BDIR)/*.so $@
rm $@
ifneq ($(SHARED),0)
mv -fv $(BDIR)/*.so $@
ln -fs $@ librapidyaml.so
else
mv -fv $(BDIR)/*.a $@
endif

ifeq ($(SHARED),0)
ifneq ($(MUSL),0)
$(RAPIDYAML_LIB): $(MUSL_DIR)
$(MUSL_DIR):
mkdir -p `dirname $(MUSL_DIR)`
cd `dirname $(MUSL_DIR)` && \
wget https://musl.cc/x86_64-linux-musl-cross.tgz && \
tar xfz x86_64-linux-musl-cross.tgz
endif
endif

$(RAPIDYAML_H): $(RAPIDYAML_JAVA)
javac -h . $^
Expand Down
34 changes: 34 additions & 0 deletions rapidyaml/native/musl.x86_64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
set(MUSL_DIR $ENV{MUSL_DIR})
set(MUSL_TGT x86_64-linux-musl)

set(MUSL_BIN ${MUSL_DIR}/bin/${MUSL_TGT}-)
set(MUSL_PFX ${MUSL_DIR}/${MUSL_TGT})
set(MUSL_LIB ${MUSL_DIR}/${MUSL_TGT}/lib)
set(MUSL_INC ${MUSL_DIR}/${MUSL_TGT}/include)

set(MUSL TRUE)

set(CMAKE_C_COMPILER ${MUSL_BIN}gcc)
set(CMAKE_CXX_COMPILER ${MUSL_BIN}g++)
set(CMAKE_AR ${MUSL_BIN}ar)
set(CMAKE_C_COMPILER_AR ${MUSL_BIN}ar)
set(CMAKE_CXX_COMPILER_AR ${MUSL_BIN}ar)
set(CMAKE_RANLIB ${MUSL_BIN}ranlib)
set(CMAKE_C_COMPILER_RANLIB ${MUSL_BIN}ranlib)
set(CMAKE_CXX_COMPILER_RANLIB ${MUSL_BIN}ranlib)
set(CMAKE_ADDR2LINE ${MUSL_BIN}addr2line)
set(CMAKE_LINKER ${MUSL_BIN}ld)
set(CMAKE_NM ${MUSL_BIN}nm)
set(CMAKE_OBJCOPY ${MUSL_BIN}objcopy)
set(CMAKE_OBJDUMP ${MUSL_BIN}objdump)
set(CMAKE_READELF ${MUSL_BIN}readelf)
set(CMAKE_STRIP ${MUSL_BIN}strip)

# set searching rules for cross-compiler
set(CMAKE_SYSTEM_PREFIX_PATH ${MUSL_PFX})
set(CMAKE_SYSTEM_LIBRARY_PATH ${MUSL_LIB})
set(CMAKE_SYSTEM_INCLUDE_PATH ${MUSL_INC})
set(CMAKE_FIND_ROOT_PATH ${MUSL_PFX})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH) # search also in the host
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) # search also in the host
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) # search also in the host
8 changes: 5 additions & 3 deletions rapidyaml/native/rapidyaml_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ int main()
failed_cases += (!tr);
printf("case %zu/%zu: %s\n", i, C4_COUNTOF(test_cases), tr ? "ok!" : "failed");
}
printf("%u/%u assertions failed\n", total.num_failed_assertions, total.num_assertions);
printf("%u/%u tests failed\n", total.num_failed_tests, total.num_tests);
printf("%zu/%zu cases failed\n", failed_cases, num_cases);
printf("assertions: %u/%u pass %u/%u fail\n", total.num_assertions - total.num_failed_assertions, total.num_assertions, total.num_failed_assertions, total.num_assertions);
printf("tests: %u/%u pass %u/%u fail\n", total.num_tests - total.num_failed_tests, total.num_tests, total.num_failed_tests, total.num_tests);
printf("cases: %zu/%zu pass %zu/%zu fail\n", num_cases-failed_cases, num_cases, failed_cases, num_cases);
if(total)
printf("TESTS SUCCEED!\n");
return total ? 0 : -1;
}
3 changes: 2 additions & 1 deletion rapidyaml/rapidyaml.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
RAPIDYAML_TAG ?= v0.7.2
RAPIDYAML_VERSION ?= 0.7.2
RAPIDYAML_TAG ?= v$(RAPIDYAML_TAG)
RAPIDYAML_REPO := https://github.com/biojppm/rapidyaml
RAPIDYAML_JAVA := \
$(ROOT)/rapidyaml/src/main/java/org/rapidyaml/Rapidyaml.java \
Expand Down

0 comments on commit f08cc57

Please sign in to comment.