| 1 |
# -*- makefile -*- |
| 2 |
|
| 3 |
BUILD_OS := $(strip $(shell uname -s | tr '[:upper:]' '[:lower:]')) |
| 4 |
OS ?= $(BUILD_OS) |
| 5 |
ifeq ($(OS),sunos) |
| 6 |
OS = solaris |
| 7 |
endif |
| 8 |
|
| 9 |
# Default value of $OS on Windows is Windows_NT |
| 10 |
ifeq ($(OS), Windows_NT) |
| 11 |
# that's how we detect x64... |
| 12 |
ifneq ($(findstring 64, $(BUILD_OS)),) |
| 13 |
OS = win64 |
| 14 |
else |
| 15 |
OS = win32 |
| 16 |
endif |
| 17 |
endif |
| 18 |
|
| 19 |
ifneq ($(findstring cygwin, $(BUILD_OS)),) |
| 20 |
# cygwin is always x32 |
| 21 |
OS = win32 |
| 22 |
endif |
| 23 |
|
| 24 |
CPU ?= $(shell uname -m | sed -e 's/i[345678]86/i386/') |
| 25 |
PLATFORM = $(CPU)-$(OS) |
| 26 |
|
| 27 |
JDK_HOME=$(shell if [ -d "$(JAVA_HOME)"/include ];then echo "$(JAVA_HOME)"; else echo "$(JAVA_HOME)"/..; fi) |
| 28 |
# Set defaults to unix (linux/solaris/bsd) |
| 29 |
PREFIX = lib |
| 30 |
JNIEXT = so |
| 31 |
|
| 32 |
export MACOSX_DEPLOYMENT_TARGET=10.4 |
| 33 |
|
| 34 |
CCACHE := $(ccache -V >/dev/null 2>&1 && echo ccache) |
| 35 |
SRC_DIR ?= $(shell pwd)/jni |
| 36 |
JNI_DIR ?= $(SRC_DIR) |
| 37 |
BUILD_DIR ?= $(shell pwd)/build |
| 38 |
|
| 39 |
JFFI_SRC_DIR = $(SRC_DIR)/jffi |
| 40 |
JFFI_BUILD_DIR = $(BUILD_DIR)/jffi |
| 41 |
|
| 42 |
ifeq ($(USE_SYSTEM_LIBFFI),1) |
| 43 |
LIBFFI_LIBS ?= $(shell pkg-config --libs libffi) |
| 44 |
LIBFFI_CFLAGS ?= $(shell pkg-config --cflags libffi) |
| 45 |
else |
| 46 |
LIBFFI_SRC_DIR = $(SRC_DIR)/libffi |
| 47 |
LIBFFI_BUILD_DIR = $(BUILD_DIR)/libffi-$(PLATFORM) |
| 48 |
LIBFFI = $(LIBFFI_BUILD_DIR)/.libs/libffi_convenience.a |
| 49 |
LIBFFI_LIBS = $(LIBFFI) |
| 50 |
LIBFFI_CFLAGS = -I"$(LIBFFI_BUILD_DIR)"/include |
| 51 |
endif |
| 52 |
|
| 53 |
SRCS = $(wildcard $(JFFI_SRC_DIR)/*.c) |
| 54 |
OBJS = $(patsubst %.c, $(JFFI_BUILD_DIR)/%.o, $(notdir $(SRCS))) |
| 55 |
|
| 56 |
vpath %.h $(JFFI_SRC_DIR) |
| 57 |
|
| 58 |
LIBNAME = jffi |
| 59 |
# |
| 60 |
# Compiler/linker flags from: |
| 61 |
# http://weblogs.java.net/blog/kellyohair/archive/2006/01/compilation_of_1.html |
| 62 |
JFLAGS = -fno-omit-frame-pointer -fno-strict-aliasing -DNDEBUG |
| 63 |
OFLAGS = -O2 $(JFLAGS) |
| 64 |
|
| 65 |
# MacOS headers aren't completely warning free, so turn them off |
| •66 |
WERROR = |
| 67 |
ifneq ($(OS),darwin) |
| 68 |
ifneq ($(OS),aix) |
| 69 |
WFLAGS += -Wundef $(WERROR) |
| 70 |
endif |
| 71 |
endif |
| 72 |
WFLAGS += -W -Wall -Wno-unused -Wno-parentheses -Wno-unused-parameter |
| 73 |
PICFLAGS = -fPIC |
| 74 |
SOFLAGS = # Filled in for each OS specifically |
| 75 |
FFI_MMAP_EXEC = -DFFI_MMAP_EXEC_WRIT |
| 76 |
|
| 77 |
FFI_CC = $(CCACHE) $(CC) |
| 78 |
FFI_LD = $(LD) |
| 79 |
FFI_CFLAGS = $(FFI_MMAP_EXEC) $(OFLAGS) |
| 80 |
STRIP ?= strip -S |
| 81 |
|
| 82 |
JDK_INCLUDES = -I"$(JDK_HOME)/include" -I"$(JDK_HOME)/include/$(OS)" |
| 83 |
IFLAGS = -I"$(BUILD_DIR)" -I"$(BUILD_DIR)"/jni -I$(SRC_DIR) -I"$(JFFI_SRC_DIR)" |
| 84 |
CFLAGS += $(OFLAGS) $(WFLAGS) $(IFLAGS) $(PICFLAGS) $(JDK_INCLUDES) $(LIBFFI_CFLAGS) |
| 85 |
CFLAGS += -D_REENTRANT -D_LARGEFILE64_SOURCE -D_GNU_SOURCE |
| 86 |
|
| 87 |
ifeq ($(OS), win64) |
| 88 |
override CPU = x86_64 |
| 89 |
JDK_INCLUDES=-I$(WIN32_JNI_INCLUDES) -I$(WIN32_JNI_INCLUDES)/win32 |
| 90 |
CC = x86_64-w64-mingw32-gcc -m64 |
| 91 |
PICFLAGS = |
| 92 |
ifneq ($(findstring cygwin, $(BUILD_OS)),) |
| 93 |
CC += -mno-cygwin |
| 94 |
LDFLAGS += -mno-cygwin |
| 95 |
endif |
| 96 |
CFLAGS += -mwin32 -D_JNI_IMPLEMENTATION_ |
| 97 |
LDFLAGS += -Wl,--add-stdcall-alias |
| 98 |
PICFLAGS= |
| 99 |
SOFLAGS += -shared -static-libgcc |
| 100 |
PREFIX = |
| 101 |
JNIEXT=dll |
| 102 |
AR = x86_64-w64-mingw32-ar |
| 103 |
LD = x86_64-w64-mingw32-ld |
| 104 |
STRIP = x86_64-w64-mingw32-strip --strip-debug |
| 105 |
CONFIGURE_BUILD = x86_64-w64-mingw32 |
| 106 |
endif |
| 107 |
|
| 108 |
ifeq ($(OS),cross-mingw32) |
| 109 |
override OS = win32 |
| 110 |
override CPU = i386 |
| 111 |
JDK_INCLUDES=-I$(JNI_DIR)/win32/include -I$(JNI_DIR)/win32/include/win32 |
| 112 |
CC = i686-w64-mingw32-gcc |
| 113 |
LD = i686-w64-mingw32-ld |
| 114 |
STRIP = i686-w64-mingw32-strip --strip-debug |
| 115 |
export RANLIB = i686-w64-mingw32-ranlib |
| 116 |
CONFIGURE_HOST = i686-mingw32 |
| 117 |
CROSS_COMPILE=1 |
| 118 |
endif |
| 119 |
|
| 120 |
|
| 121 |
ifeq ($(OS),cross-win64) |
| 122 |
override CPU = x86_64 |
| 123 |
override OS = win64 |
| 124 |
JDK_INCLUDES=-I$(JNI_DIR)/win32/include -I$(JNI_DIR)/win32/include/win32 |
| 125 |
CC = x86_64-w64-mingw32-gcc |
| 126 |
LD = x86_64-w64-mingw32-ld |
| 127 |
STRIP = x86_64-w64-mingw32-strip --strip-debug |
| 128 |
AR = x86_64-w64-mingw32-ar |
| 129 |
LD = x86_64-w64-mingw32-ld |
| 130 |
export RANLIB = x86_64-w64-mingw32-ranlib |
| 131 |
CONFIGURE_HOST = x86_64-w64-mingw32 |
| 132 |
PICFLAGS = |
| 133 |
CROSS_COMPILE=1 |
| 134 |
SOFLAGS += -shared -static-libgcc |
| 135 |
PREFIX = |
| 136 |
JNIEXT=dll |
| 137 |
endif |
| 138 |
|
| 139 |
|
| 140 |
ifneq ($(findstring cygwin,$(BUILD_OS)),) |
| 141 |
OS = win32 |
| 142 |
JAVA_HOME := $(shell cygpath -u $(JAVA_HOME)) |
| 143 |
endif |
| 144 |
|
| 145 |
ifeq ($(OS), win32) |
| 146 |
ifneq ($(findstring cygwin, $(BUILD_OS)),) |
| 147 |
CC += -mno-cygwin |
| 148 |
LDFLAGS += -mno-cygwin |
| 149 |
endif |
| 150 |
CFLAGS += -mwin32 -D_JNI_IMPLEMENTATION_ |
| 151 |
# Linking against CRTMT.O is a workaround for GCC 4.4 bug |
| 152 |
# that ignores __thread (which we really need for errno handling). |
| 153 |
# See: http://n2.nabble.com/gcc-4-4-multi-threaded-exception-handling-thread-specifier-not-working-td3440749.html |
| 154 |
ifeq ($(findstring -w64-,$(CC)),) |
| 155 |
CRTMT.O = $(shell $(CC) -print-file-name=crtmt.o) |
| 156 |
endif |
| 157 |
LDFLAGS += -Wl,--add-stdcall-alias $(CRTMT.O) |
| 158 |
PICFLAGS= |
| 159 |
SOFLAGS += -shared -static-libgcc |
| 160 |
PREFIX = |
| 161 |
JNIEXT=dll |
| 162 |
endif |
| 163 |
|
| 164 |
ifeq ($(OS), darwin) |
| 165 |
PLATFORM = darwin |
| 166 |
ARCHES = x86_64 arm64 |
| 167 |
|
| 168 |
XCODE=$(shell xcode-select -print-path) |
| 169 |
CC = $(XCODE)/usr/bin/gcc |
| 170 |
MACSDK = /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk |
| 171 |
|
| 172 |
CFLAGS += -isysroot $(MACSDK) -DTARGET_RT_MAC_CFM=0 |
| 173 |
CFLAGS += -I$(MACSDK)/System/Library/Frameworks/JavaVM.framework/Headers |
| 174 |
CFLAGS += $(foreach arch, $(ARCHES),-arch $(arch)) |
| 175 |
LDFLAGS = $(foreach arch, $(ARCHES),-arch $(arch)) -dynamiclib \ |
| 176 |
-Wl,-syslibroot,$(MACSDK) -mmacosx-version-min=10.6 |
| 177 |
JNIEXT = jnilib |
| 178 |
#CFLAGS += -I$(MACSDK)/System/Library/Frameworks/Kernel.framework/Versions/A/Headers |
| 179 |
CFLAGS += -fexceptions |
| 180 |
FFI_CFLAGS += -isysroot $(MACSDK) -I$(MACSDK)/usr/include |
| 181 |
PICFLAGS = |
| 182 |
SOFLAGS = |
| 183 |
#LIBS += $(JAVA_HOME)/jre/lib/libjsig.dylib |
| 184 |
OBJS += $(JFFI_BUILD_DIR)/darwin-longjmp.o $(JFFI_BUILD_DIR)/darwin-trampoline.o |
| 185 |
endif |
| 186 |
|
| 187 |
ifeq ($(OS), linux) |
| 188 |
SOFLAGS = -shared -static-libgcc -Wl,-soname,$(@F) -Wl,-O1 |
| 189 |
CFLAGS += -pthread |
| 190 |
endif |
| 191 |
|
| 192 |
ifeq ($(OS), solaris) |
| 193 |
CC = gcc |
| 194 |
CFLAGS += -D__EXTENSIONS__ -std=c99 |
| 195 |
LD = /usr/ccs/bin/ld |
| 196 |
SOFLAGS = -shared -static-libgcc |
| 197 |
LIBS += -ldl |
| 198 |
STRIP = strip |
| 199 |
endif |
| 200 |
|
| 201 |
ifeq ($(OS), aix) |
| 202 |
SOFLAGS = -shared -static-libgcc |
| 203 |
CFLAGS += -pthread |
| 204 |
LDFLAGS += -pthread |
| 205 |
JNIEXT = a |
| 206 |
STRIP = strip |
| 207 |
endif |
| 208 |
ifeq ($(OS), os400) |
| 209 |
SOFLAGS = -shared -static-libgcc |
| 210 |
CFLAGS += -pthread -D_CALL_ELF=0 |
| 211 |
LDFLAGS += -pthread |
| 212 |
JNIEXT = so |
| 213 |
STRIP = strip |
| 214 |
LIBFFI = /QOpenSys/pkgs/lib/libffi.so |
| 215 |
LIBFFI_LIBS ?= $(shell pkg-config --libs libffi) |
| 216 |
LIBFFI_CFLAGS ?= $(shell pkg-config --cflags libffi) |
| 217 |
PLATFORM = aix |
| 218 |
ARCHES = ppc64 |
| 219 |
WFLAGS += -Werror=undef |
| 220 |
endif |
| 221 |
|
| 222 |
ifneq ($(OS), dragonflybsd) |
| 223 |
SOFLAGS = -shared |
| 224 |
endif |
| 225 |
|
| 226 |
ifneq ($(findstring bsd, $(OS)),) |
| 227 |
SOFLAGS = -shared -static-libgcc |
| 228 |
CFLAGS += -pthread |
| 229 |
LDFLAGS += -pthread |
| 230 |
endif |
| 231 |
|
| 232 |
ifeq ($(CPU), i386) |
| 233 |
ifneq ($(findstring $(OS), linux),) |
| 234 |
CFLAGS += -march=i586 -mtune=generic |
| 235 |
endif |
| 236 |
endif |
| 237 |
|
| 238 |
LIBJFFI = $(BUILD_DIR)/$(PREFIX)$(LIBNAME)-$(VERSION).$(JNIEXT) |
| 239 |
|
| 240 |
LIBFFI_CONFIGURE = $(LIBFFI_SRC_DIR)/configure --disable-static \ |
| 241 |
--with-pic=yes --disable-dependency-tracking |
| 242 |
ifdef CONFIGURE_HOST |
| 243 |
LIBFFI_CONFIGURE += --host=$(CONFIGURE_HOST) |
| 244 |
endif |
| 245 |
|
| 246 |
ifdef CONFIGURE_BUILD |
| 247 |
LIBFFI_CONFIGURE += --build=$(CONFIGURE_BUILD) |
| 248 |
endif |
| 249 |
|
| 250 |
# Disable memfd_create which binds us to newer glibc (jnr/jffi#138) |
| 251 |
LIBFFI_CONFIGURE += 'ac_cv_func_memfd_create=no' |
| 252 |
|
| 253 |
all: $(LIBJFFI) |
| 254 |
|
| 255 |
debug: |
| 256 |
@echo OS="$(OS)" |
| 257 |
@echo BUILD_OS="$(BUILD_OS)" |
| 258 |
@echo CPU="$(CPU)" |
| 259 |
@echo JAVA_HOME="$(JAVA_HOME)" |
| 260 |
@echo JDK_HOME="$(JDK_HOME)" |
| 261 |
@echo "PLATFORM=$(PLATFORM)" |
| 262 |
@echo "JFFI_BUILD_DIR=$(JFFI_BUILD_DIR)" |
| 263 |
@echo "OBJS=$(OBJS)" |
| 264 |
|
| 265 |
$(LIBJFFI): $(OBJS) $(LIBFFI_LIBS) |
| 266 |
$(CC) -o $@ $(LDFLAGS) $(SOFLAGS) $(OBJS) $(LIBFFI_LIBS) $(LIBS) |
| 267 |
$(STRIP) $@ |
| 268 |
ifeq ($(OS), darwin) |
| 269 |
codesign -s - $@ |
| 270 |
endif |
| 271 |
|
| 272 |
$(BUILD_DIR)/%.o : $(SRC_DIR)/%.c $(wildcard $(JFFI_SRC_DIR)/*.h) |
| 273 |
@mkdir -p $(@D) |
| 274 |
@$(CCACHE) $(CC) $(CFLAGS) -c $< -o $@ |
| 275 |
|
| 276 |
$(BUILD_DIR)/%.o : $(SRC_DIR)/%.S $(wildcard $(JFFI_SRC_DIR)/*.h) |
| 277 |
@mkdir -p $(@D) |
| 278 |
@$(CC) $(CFLAGS) -o $@ -c $< |
| 279 |
|
| 280 |
$(OBJS) : $(LIBFFI_LIBS) |
| 281 |
|
| 282 |
ifeq ($(OS), darwin) |
| 283 |
build_ffi = \ |
| 284 |
mkdir -p $(BUILD_DIR)/libffi-darwin-$(1); \ |
| 285 |
(if [ ! -f $(BUILD_DIR)/libffi-darwin-$(1)/Makefile ]; then \ |
| 286 |
echo "Configuring libffi for $(1)"; \ |
| 287 |
cd $(BUILD_DIR)/libffi-darwin-$(1) && \ |
| 288 |
env CC="$(CCACHE) $(CC)" CFLAGS="-arch $(1) $(FFI_CFLAGS)" LDFLAGS="-arch $(1)" \ |
| 289 |
$(LIBFFI_CONFIGURE) --host=$(1)-apple-darwin > /dev/null; \ |
| 290 |
fi); \ |
| 291 |
env MACOSX_DEPLOYMENT_TARGET=10.4 $(MAKE) -C $(BUILD_DIR)/libffi-darwin-$(1) |
| 292 |
|
| 293 |
$(LIBFFI): |
| 294 |
@mkdir -p $(@D) |
| 295 |
@for arch in $(ARCHES); do $(call build_ffi,$$arch);done |
| 296 |
# Assemble into a FAT (x86_64, arm64) library |
| 297 |
@mkdir -p $(BUILD_DIR)/libffi/.libs |
| 298 |
@env MACOSX_DEPLOYMENT_TARGET=10.4 /usr/bin/libtool -static -o $@ \ |
| 299 |
$(foreach arch, $(ARCHES),$(BUILD_DIR)/libffi-darwin-$(arch)/.libs/libffi_convenience.a) |
| 300 |
@mkdir -p $(LIBFFI_BUILD_DIR)/include |
| 301 |
$(RM) $(LIBFFI_BUILD_DIR)/include/ffi.h |
| 302 |
@( \ |
| 303 |
printf "#if defined(__i386__)\n"; \ |
| 304 |
printf "#include \"libffi-darwin-i386/include/ffi.h\"\n"; \ |
| 305 |
printf "#elif defined(__x86_64__)\n"; \ |
| 306 |
printf "#include \"libffi-darwin-x86_64/include/ffi.h\"\n";\ |
| 307 |
printf "#elif defined(__arm64__)\n"; \ |
| 308 |
printf "#include \"libffi-darwin-arm64/include/ffi.h\"\n";\ |
| 309 |
printf "#endif\n";\ |
| 310 |
) > $(LIBFFI_BUILD_DIR)/include/ffi.h |
| 311 |
@( \ |
| 312 |
printf "#if defined(__i386__)\n"; \ |
| 313 |
printf "#include \"libffi-darwin-i386/include/ffitarget.h\"\n"; \ |
| 314 |
printf "#elif defined(__x86_64__)\n"; \ |
| 315 |
printf "#include \"libffi-darwin-x86_64/include/ffitarget.h\"\n";\ |
| 316 |
printf "#elif defined(__arm64__)\n"; \ |
| 317 |
printf "#include \"libffi-darwin-arm64/include/ffitarget.h\"\n";\ |
| 318 |
printf "#endif\n";\ |
| 319 |
) > $(LIBFFI_BUILD_DIR)/include/ffitarget.h |
| 320 |
else |
| 321 |
$(LIBFFI): |
| 322 |
@mkdir -p $(LIBFFI_BUILD_DIR) |
| 323 |
@if [ ! -f $(LIBFFI_BUILD_DIR)/Makefile ]; then \ |
| 324 |
echo "Configuring libffi for $(PLATFORM)"; \ |
| 325 |
cd $(LIBFFI_BUILD_DIR) && env CC="$(FFI_CC)" LD="$(FFI_LD)" CFLAGS="$(FFI_CFLAGS)" \ |
| 326 |
$(LIBFFI_CONFIGURE) > /dev/null; \ |
| 327 |
fi |
| 328 |
$(MAKE) -C $(LIBFFI_BUILD_DIR) |
| 329 |
endif |
| 330 |
|
| 331 |
|
| 332 |
clean:: |
| 333 |
# nothing to do - ant will delete the build dir |
| 334 |
|