ohair@425: # ohair@425: # Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. ohair@425: # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@425: # ohair@425: # This code is free software; you can redistribute it and/or modify it ohair@425: # under the terms of the GNU General Public License version 2 only, as ohair@425: # published by the Free Software Foundation. Oracle designates this ohair@425: # particular file as subject to the "Classpath" exception as provided ohair@425: # by Oracle in the LICENSE file that accompanied this code. ohair@425: # ohair@425: # This code is distributed in the hope that it will be useful, but WITHOUT ohair@425: # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@425: # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@425: # version 2 for more details (a copy is included in the LICENSE file that ohair@425: # accompanied this code). ohair@425: # ohair@425: # You should have received a copy of the GNU General Public License version ohair@425: # 2 along with this work; if not, write to the Free Software Foundation, ohair@425: # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@425: # ohair@425: # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@425: # or visit www.oracle.com if you need additional information or have any ohair@425: # questions. ohair@425: # ohair@425: ohair@425: # When you read this source. Remember that $(sort ...) has the side effect ohair@425: # of removing duplicates. It is actually this side effect that is ohair@425: # desired whenever sort is used below! ohair@425: ohair@425: ifeq (,$(_MAKEBASE_GMK)) erikj@445: $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk) ohair@425: endif ohair@425: ohair@425: ifeq ($(COMPILER_TYPE),CC) ohair@425: COMPILING_MSG=echo Compiling $1 ohair@425: LINKING_MSG=echo Linking $1 ohair@425: LINKING_EXE_MSG=echo Linking executable $1 ohair@425: ARCHIVING_MSG=echo Archiving $1 ohair@425: else ohair@425: COMPILING_MSG= ohair@425: LINKING_MSG= ohair@425: LINKING_EXE_MSG= ohair@425: ARCHIVING_MSG= ohair@425: endif ohair@425: ohair@425: define add_native_source ohair@425: # param 1 = BUILD_MYPACKAGE ohair@425: # parma 2 = the source file name (..../alfa.c or .../beta.cpp) ohair@425: # param 3 = the bin dir that stores all .o (.obj) and .d files. ohair@425: # param 4 = the c flags to the compiler ohair@425: # param 5 = the c compiler ohair@425: # param 6 = the c++ flags to the compiler ohair@425: # param 7 = the c++ compiler ohair@425: ohair@425: ifneq (,$$(filter %.c,$2)) ohair@425: # Compile as a C file erikj@445: $1_$2_FLAGS=$4 $$($1_$(notdir $2)_CFLAGS) ohair@425: $1_$2_COMP=$5 erikj@445: $1_$2_DEP_FLAG:=$(C_FLAG_DEPS) erikj@445: else ifneq (,$$(filter %.m,$2)) erikj@445: # Compile as a objective-c file erikj@445: $1_$2_FLAGS=-x objective-c $4 $$($1_$(notdir $2)_CFLAGS) erikj@445: $1_$2_COMP=$5 erikj@445: $1_$2_DEP_FLAG:=$(C_FLAG_DEPS) ohair@425: else ohair@425: # Compile as a C++ file erikj@445: $1_$2_FLAGS=$6 $$($1_$(notdir $2)_CXXFLAGS) ohair@425: $1_$2_COMP=$7 erikj@445: $1_$2_DEP_FLAG:=$(CXX_FLAG_DEPS) ohair@425: endif ohair@425: # Generate the .o (.obj) file name and place it in the bin dir. erikj@445: $1_$2_OBJ:=$3/$$(patsubst %.cpp,%$(OBJ_SUFFIX),$$(patsubst %.c,%$(OBJ_SUFFIX),$$(patsubst %.m,%$(OBJ_SUFFIX),$$(notdir $2)))) ohair@425: # Only continue if this object file hasn't been processed already. This lets the first found ohair@425: # source file override any other with the same name. ohair@425: ifeq (,$$(findstring $$($1_$2_OBJ),$$($1_OBJS_SO_FAR))) ohair@425: $1_OBJS_SO_FAR+=$$($1_$2_OBJ) ohair@425: # And this is the dependency file for this obj file. ohair@425: $1_$2_DEP:=$$(patsubst %$(OBJ_SUFFIX),%.d,$$($1_$2_OBJ)) ohair@425: # Include previously generated dependency information. (if it exists) ohair@425: -include $$($1_$2_DEP) ohair@425: ohair@425: ifeq ($(COMPILER_TYPE),CL) ohair@425: $1_$2_DEBUG_OUT_FLAGS:=-Fd$$(patsubst %$(OBJ_SUFFIX),%.pdb,$$($1_$2_OBJ)) \ ohair@425: -Fm$$(patsubst %$(OBJ_SUFFIX),%.map,$$($1_$2_OBJ)) ohair@425: endif ohair@425: ohair@425: $$($1_$2_OBJ) : $2 ohair@425: ifeq ($(COMPILER_TYPE),CC) ohair@425: $$(call COMPILING_MSG,$$(notdir $2)) erikj@445: $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP) -c $(CC_OUT_OPTION)$$($1_$2_OBJ) $2 ohair@425: endif ohair@425: ifeq ($(COMPILER_TYPE),CL) ohair@425: $$(call COMPILING_MSG,$$(notdir $2)) ohair@425: $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEBUG_OUT_FLAGS) -c $(CC_OUT_OPTION)$$($1_$2_OBJ) $2 ohair@425: endif ohair@425: endif ohair@425: endef ohair@425: ohair@425: define SetupNativeCompilation ohair@425: # param 1 is for example BUILD_MYPACKAGE ohair@425: # param 2,3,4,5,6,7,8 are named args. ohair@425: # SRC one or more directory roots to scan for C/C++ files. ohair@425: # LANG C or C++ ohair@425: # CFLAGS the compiler flags to be used, used both for C and C++. ohair@425: # CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS. ohair@425: # LDFLAGS the linker flags to be used, used both for C and C++. ohair@425: # LDFLAGS_SUFFIX the linker flags to be added last on the commandline ohair@425: # typically the libraries linked to. ohair@425: # ARFLAGS the archiver flags to be used ohair@425: # BIN the directory where we store the object files ohair@425: # LIB the resulting library file ohair@425: # EXE the resulting exec file ohair@425: # INCLUDES only pick source from these directories ohair@425: # EXCLUDES do not pick source from these directories ohair@425: # INCLUDE_FILES only compile exactly these files! ohair@425: # EXCLUDE_FILES with these names ohair@425: # VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run ohair@425: # RC_FLAGS flags for RC. erikj@445: # MAPFILE mapfile erikj@445: # REORDER reorder file ohair@425: $(if $2,$1_$(strip $2)) ohair@425: $(if $3,$1_$(strip $3)) ohair@425: $(if $4,$1_$(strip $4)) ohair@425: $(if $5,$1_$(strip $5)) ohair@425: $(if $6,$1_$(strip $6)) ohair@425: $(if $7,$1_$(strip $7)) ohair@425: $(if $8,$1_$(strip $8)) ohair@425: $(if $9,$1_$(strip $9)) ohair@425: $(if $(10),$1_$(strip $(10))) ohair@425: $(if $(11),$1_$(strip $(11))) ohair@425: $(if $(12),$1_$(strip $(12))) ohair@425: $(if $(13),$1_$(strip $(13))) ohair@425: $(if $(14),$1_$(strip $(14))) ohair@425: $(if $(15),$1_$(strip $(15))) ohair@425: $(if $(16),$1_$(strip $(16))) ohair@425: $(if $(17),$1_$(strip $(17))) ohair@425: $(if $(18),$1_$(strip $(18))) ohair@425: $(if $(19),$1_$(strip $(19))) ohair@425: $(if $(20),$1_$(strip $(20))) ohair@425: ohair@425: ifeq (,$$($1_LANG)) ohair@425: $$(error You have to specify LANG for native compilation $1) ohair@425: endif ohair@425: ifeq (C,$$($1_LANG)) ohair@425: $1_LD:=$(LD) ohair@425: $1_LDEXE:=$(LDEXE) ohair@425: else ohair@425: ifeq (C++,$$($1_LANG)) ohair@425: $1_LD:=$(LDCXX) ohair@425: $1_LDEXE:=$(LDEXECXX) ohair@425: else ohair@425: $$(error Unknown native language $$($1_LANG) for $1) ohair@425: endif ohair@425: endif ohair@425: ohair@425: # Make sure the dirs exist. ohair@425: $$(shell $(MKDIR) -p $$($1_SRC) $$($1_BIN) $$(dir $$($1_LIB)) $$(dir $$($1_EXE))) ohair@425: # Find all files in the source trees. Sort to remove duplicates. ohair@425: $1_ALL_SRCS := $$(sort $$(foreach i,$$($1_SRC), $$(shell $(FIND) $$i -type f))) ohair@425: # Extract the C/C++ files. ohair@425: $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES))) ohair@425: $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES))) ohair@425: ifneq ($$($1_EXCLUDE_FILES),) ohair@425: $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES)) ohair@425: endif erikj@445: $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter %.c %.cpp %.m,$$($1_ALL_SRCS))) ohair@425: ifneq (,$$(strip $$($1_INCLUDE_FILES))) ohair@425: $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS)) ohair@425: endif ohair@425: ifeq (,$$($1_SRCS)) ohair@425: $$(error No sources found for $1 when looking inside the dirs $$($1_SRC)) ohair@425: endif ohair@425: # There can be only a single bin dir root, no need to foreach over the roots. ohair@425: $1_BINS := $$(wildcard $$($1_BIN)/*$(OBJ_SUFFIX)) ohair@425: # Now we have a list of all c/c++ files to compile: $$($1_SRCS) ohair@425: # and we have a list of all existing object files: $$($1_BINS) ohair@425: ohair@425: # Prepend the source/bin path to the filter expressions. Then do the filtering. ohair@425: ifneq ($$($1_INCLUDES),) ohair@425: $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES)))) ohair@425: $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS)) ohair@425: endif ohair@425: ifneq ($$($1_EXCLUDES),) ohair@425: $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES)))) ohair@425: $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS)) ohair@425: endif ohair@425: ohair@425: # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides ohair@425: # a reproducable order on the input files to the linker). erikj@445: $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_BIN)/,$$(patsubst %.cpp,%$(OBJ_SUFFIX),$$(patsubst %.c,%$(OBJ_SUFFIX),$$(patsubst %.m,%$(OBJ_SUFFIX),$$(notdir $$($1_SRCS))))))) ohair@425: $1 := $$($1_EXPECTED_OBJS) ohair@425: # Are there too many object files on disk? Perhaps because some source file was removed? ohair@425: $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS))) ohair@425: # Clean out the superfluous object files. ohair@425: $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS)) ohair@425: ohair@425: # Pickup extra HOST_OS_API and/or PLATFORM dependent variables for CFLAGS. ohair@425: $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(HOST_OS_API)) $$($1_CFLAGS_$(PLATFORM)) ohair@425: ifneq ($(DEBUG_LEVEL),release) ohair@425: # Pickup extra debug dependent variables for CFLAGS ohair@425: $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug) ohair@425: $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(HOST_OS_API)_debug) ohair@425: $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(PLATFORM)_debug) ohair@425: else ohair@425: $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release) ohair@425: $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(HOST_OS_API)_release) ohair@425: $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(PLATFORM)_release) ohair@425: endif ohair@425: ohair@425: # Pickup extra HOST_OS_API and/or PLATFORM dependent variables for CXXFLAGS. ohair@425: $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(HOST_OS_API)) $$($1_CXXFLAGS_$(PLATFORM)) ohair@425: ifneq ($(DEBUG_LEVEL),release) ohair@425: # Pickup extra debug dependent variables for CXXFLAGS ohair@425: $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug) ohair@425: $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(HOST_OS_API)_debug) ohair@425: $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(PLATFORM)_debug) ohair@425: else ohair@425: $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release) ohair@425: $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(HOST_OS_API)_release) ohair@425: $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(PLATFORM)_release) ohair@425: endif ohair@425: ohair@425: ifeq ($$($1_CXXFLAGS),) ohair@425: $1_CXXFLAGS:=$$($1_CFLAGS) ohair@425: endif ohair@425: ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),) ohair@425: $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS) ohair@425: endif ohair@425: erikj@445: ifneq (,$$($1_REORDER)) erikj@445: $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER) erikj@445: $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER) erikj@445: endif erikj@445: ohair@425: # Now create a list of the packages that are about to compile. Used when sending source ohair@425: # in a batch to the compiler. ohair@425: $$(shell $(RM) $$($1_BIN)/_the.list_of_sources) ohair@425: $$(eval $$(call ListPathsSafelyNow,$1_SRCS,\n, >> $$($1_BIN)/_the.list_of_sources)) ohair@425: ohair@425: # Now call add_native_source for each source file we are going to compile. ohair@425: $$(foreach p,$$($1_SRCS),\ ohair@425: $$(eval $$(call add_native_source,$1,$$p,$$($1_BIN),\ ohair@425: $$($1_CFLAGS) $$($1_EXTRA_CFLAGS),$(CC),\ ohair@425: $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS),$(CXX)))) ohair@425: ohair@425: # On windows we need to create a resource file ohair@425: ifeq ($(HOST_OS_API), winapi) ohair@425: ifneq (,$$($1_VERSIONINFO_RESOURCE)) ohair@425: ifneq (,$$($1_LIB)) erikj@445: $1_BASENAME:=$$(basename $$(notdir $$($1_LIB))) ohair@425: endif ohair@425: ifneq (,$$($1_EXE)) erikj@445: $1_BASENAME:=$$(basename $$(notdir $$($1_EXE))) ohair@425: endif erikj@445: $1_RES:=$$($1_BIN)/$$($1_BASENAME).res ohair@425: $$($1_RES): $$($1_VERSIONINFO_RESOURCE) ohair@425: $(RC) $$($1_RC_FLAGS) $(CC_OUT_OPTION)$$@ $$($1_VERSIONINFO_RESOURCE) ohair@425: endif erikj@445: ifneq (,$$($1_MANIFEST)) erikj@445: $1_PROGRAM:=$$(basename $$(notdir $$($1_EXE))) erikj@445: $1_GEN_MANIFEST:=$$($1_BIN)/$$($1_PROGRAM).manifest erikj@445: IMVERSIONVALUE:=$(JDK_MINOR_VERSION).$(JDK_MICRO_VERSION).$(JDK_UPDATE_VERSION).$(COOKED_BUILD_NUMBER) erikj@445: $$($1_GEN_MANIFEST): $$($1_MANIFEST) erikj@445: $(SED) 's%IMVERSION%$$(IMVERSIONVALUE)%g;s%PROGRAM%$$($1_PROGRAM)%g' $$< > $$@ erikj@445: endif erikj@445: endif erikj@445: erikj@445: # mapfile doesnt seem to be implemented on macosx (yet??) erikj@445: ifneq ($(HOST_OS),macosx) erikj@445: ifneq ($(HOST_OS),windows) erikj@445: $1_REAL_MAPFILE := $$($1_MAPFILE) erikj@445: ifneq (,$$($1_REORDER)) erikj@445: $1_REAL_MAPFILE := $$($1_BIN)/mapfile erikj@445: erikj@445: $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER) erikj@445: $$(MKDIR) -p $$(@D) erikj@445: $$(CP) $$($1_MAPFILE) $$@.tmp erikj@445: $$(SED) -e 's=OUTPUTDIR=$$($1_BIN)=' $$($1_REORDER) >> $$@.tmp erikj@445: $$(MV) $$@.tmp $$@ erikj@445: endif erikj@445: endif ohair@425: endif ohair@425: ohair@425: # Pickup extra HOST_OS_API dependent variables (posix or winapi) and ohair@425: # (linux,solaris,windows,bsd) for LDFLAGS and LDFLAGS_SUFFIX ohair@425: $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(HOST_OS_API)) $$($1_LDFLAGS_$(PLATFORM)) ohair@425: $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(HOST_OS_API)) $$($1_LDFLAGS_SUFFIX_$(PLATFORM)) erikj@445: ifneq (,$$($1_REAL_MAPFILE)) erikj@445: $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE)) erikj@445: endif erikj@445: ohair@425: ifneq (,$$($1_LIB)) ohair@425: ifeq (dynamic,$$(patsubst %$(SHARED_LIBRARY_SUFFIX),dynamic,$$($1_LIB))) ohair@425: # Generating a dynamic library. ohair@425: $1_EXTRA_LDFLAGS+=$$(call SET_SHARED_LIBRARY_NAME,$$(notdir $$($1_LIB))) erikj@445: $$($1_LIB) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE) ohair@425: $$(call LINKING_MSG,$$(notdir $$($1_LIB))) ohair@425: $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(LD_OUT_OPTION)$$($1_LIB) \ ohair@425: $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) ohair@425: else ohair@425: # Generating a static library, ie object file archive. ohair@425: $$($1_LIB) : $$($1_EXPECTED_OBJS) $$($1_RES) ohair@425: $$(call ARCHIVING_MSG,$$(notdir $$($1_LIB))) ohair@425: $(AR) $$($1_AR_FLAGS) $(AR_OUT_OPTION)$$($1_LIB) $$($1_EXPECTED_OBJS) \ ohair@425: $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX) ohair@425: endif ohair@425: endif ohair@425: ifneq (,$$($1_EXE)) ohair@425: # A executable binary has been specified, setup the target for it. erikj@445: $$($1_EXE) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_GEN_MANIFEST) ohair@425: $$(call LINKING_EXE_MSG,$$(notdir $$($1_EXE))) ohair@425: $$($1_LDEXE) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(EXE_OUT_OPTION)$$($1_EXE) \ erikj@445: $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) \ erikj@445: $$($1_EXTRA_LDFLAGS_SUFFIX) erikj@445: ifneq (,$$($1_GEN_MANIFEST)) erikj@445: $(MT) -nologo /manifest $$($1_GEN_MANIFEST) /outputresource:$$@;#1 erikj@445: endif ohair@425: endif ohair@425: endef