Makefile 4.97 KB
Newer Older
1 2 3 4
# Import local variable definitions
-include Makefile.local

INCLUDE_DIR ?= ../src-jdk-include
5
LIBRARY_BASE ?= dislagent
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

ifeq ($(OS),Windows_NT)
    ifeq (,$(findstring /cygdrive/,$(PATH)))
        $(error Building on the Windows platform is only supported under Cygwin)
    endif

    HOST_ENV := Cygwin
else
    HOST_ENV := $(shell uname -s)
    HOST_CPU := $(shell uname -m)
endif


TARGET_ENV ?= $(HOST_ENV)
TARGET_CPU ?= $(HOST_CPU)

22 23 24 25
LIBRARY_WINDOWS = $(LIBRARY_BASE).dll
LIBRARY_MACOSX = lib$(LIBRARY_BASE).jnilib
LIBRARY_LINUX = lib$(LIBRARY_BASE).so

26
ifneq ($(HOST_ENV),$(TARGET_ENV))
27 28
    ifeq (,$(filter MinGW DarwinX,$(TARGET_ENV)))
        $(error Cross-compiling is only supported for TARGET_ENV=MinGW or TARGET_ENV=DarwinX)
29 30 31
    endif
endif

32 33 34 35 36 37 38

# Default compiler and linker flags to produce a shared library.
# These should be redefined for platform-specific toolchains.

LD_SHARED := -shared
CC_SHARED := -shared

39 40
ifneq (,$(filter MinGW Cygwin,$(TARGET_ENV)))
    JDK_TARGET := windows
41 42
    LIB_PREFIX :=
    LIB_SUFFIX := .dll
43 44 45

    ifeq (MinGW,$(TARGET_ENV))
        CC := $(TARGET_CPU)-w64-mingw32-gcc
46
        LD := $(TARGET_CPU)-w64-mingw32-ld
47

48
        CFLAGS += -DMINGW -mthreads -fPIC
49 50
        CFLAGS_WHOLE := -flto -fwhole-program

51 52 53
        LDFLAGS += --kill-at -output-def $(LIBRARY).def
        LIBDIRS += -L=/mingw/lib -L$(dir $(shell $(CC) -print-libgcc-file-name))
        LIBS += -lws2_32 -lmingwex -lmingw32 -lcrtdll -lmsvcrt -lkernel32 -lgcc
54 55
    endif

56
else ifneq (,$(filter DarwinX Darwin,$(TARGET_ENV)))
57
    JDK_TARGET := macosx
58 59
    LIB_PREFIX := lib
    LIB_SUFFIX := .jnilib
60

61 62
    ifeq (DarwinX,$(TARGET_ENV))
        CC := darwinx-gcc
63
        LD := darwinx-ld
64
        CFLAGS += -DDARWINX
65 66
    else
        LD_SHARED := -dylib
67 68
    endif

69 70
else ifeq (Linux,$(TARGET_ENV))
    JDK_TARGET := linux
71 72
    LIB_PREFIX := lib
    LIB_SUFFIX := .so
73

74 75 76 77 78 79
    #
    # Despite the GCC documentation saying that -flto should not be used with
    # -fwhole-program, the option needs to be there, otherwise GCC produces a
    # library with undefined symbols (bypass bytecode) despite them being present.
    #
    CFLAGS_WHOLE := -flto -fwhole-program
80 81 82 83 84

    ifneq (x86_64,$(TARGET_CPU))
        CFLAGS += -m32
        LDFLAGS += -melf_i386
    endif
85 86 87 88 89
else
    $(error Target environment $(TARGET_ENV) is not supported)
endif


90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
ifeq (x86_64,$(TARGET_CPU))
    BUILD_DIR := $(JDK_TARGET)-x86_64
else
    BUILD_DIR := $(JDK_TARGET)-x86
endif

#
# TARGET_DIR defaults to BUILD_DIR, but it can be overriden from outside.
# However, the Makefile is not responsible for creating it, i.e., it has to exist.
# BUILD_DIR, on the other hand, is created by the Makefile.
#
TARGET_DIR ?= $(BUILD_DIR)


LIBRARY_NAME := $(LIB_PREFIX)$(LIBRARY_BASE)$(LIB_SUFFIX)
LIBRARY := $(TARGET_DIR)/$(LIBRARY_NAME)


108 109
# Source and object files needed to create the library
SOURCES = bytecode.c common.c jvmtiutil.c connection.c \
110
	connpool.c msgchannel.c network.c classparser.c \
Vít Kabele's avatar
Vít Kabele committed
111 112
	dislagent.c sessions.c \
	protocol/protobuf-c.c protocol/main.pb-c.c protocol/disl.pb-c.c protocol/shvm.pb-c.c
113

Vít Kabele's avatar
Vít Kabele committed
114 115
HEADERS = $(wildcard *.h) codeflags.h $(wildcard protocol/*.h)
GENSRCS = bytecode.c codeflags.h
116

117
OBJECTS = $(addprefix $(BUILD_DIR)/,$(SOURCES:%.c=%.o))
118
SRCDEPS = $(SOURCES:%.c=%.d)
119 120 121 122


# Base options depending on the build
ifeq (,$(DEBUG))
123
    CFLAGS += -O3 -DNDEBUG
124 125 126 127
else
    CFLAGS += -g3 -DDEBUG
endif

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
# Generate position independent code
CFLAGS += -fPIC

# Path to JNI and JVMTI includes
CFLAGS += -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/$(JDK_TARGET)

# Use GNU extensions and warn on most things
CFLAGS += -std=gnu99 -W -Wall -Wextra -Wno-unused-parameter

# Hide most symbols except those exported
CFLAGS_PARTS += -fvisibility=hidden


# Derive GCC flags for linker from LDFLAGS
COMMA := ,
CFLAGS_LD := $(CFLAGS_LD) $(addprefix -Wl$(COMMA),$(LDFLAGS))

145
#
146
# Link non-MinGW targets with the pthread library.
147 148 149 150 151 152 153 154 155 156
#
ifneq (MinGW,$(TARGET_ENV))
    LIBS += -lpthread
endif


# Fix-up quirks before first target
-include Makefile.quirks


157
# Default goal
158

159
.PHONY: agent
160
agent: $(GENSRCS) $(BUILD_DIR) $(LIBRARY)
161 162


163
# Convenience targets
164

165 166 167
.PHONY: whole
whole:
	@$(MAKE) WHOLE=1
168

169 170 171
.PHONY: debug
debug:
	@$(MAKE) DEBUG=1
172

173

174 175
# Generated files

176 177
bytecode.c codeflags.h:
	ant -f ../build.xml prepare-disl-agent
178

179 180 181 182 183

# Output directory for compiled object files

$(BUILD_DIR):
	mkdir $@
Vít Kabele's avatar
Vít Kabele committed
184
	mkdir $@/protocol
185 186


187 188 189 190
# Compilation and linking targets

ifneq (,$(WHOLE))

191
CFLAGS += $(CFLAGS_WHOLE) -DWHOLE
192 193

$(LIBRARY): $(HEADERS) $(SOURCES)
194
	$(CC) $(CC_SHARED) $(CFLAGS) $(TARGET_ARCH) $(CFLAGS_LD) $(SOURCES) $(LIBS) $(OUTPUT_OPTION)
195 196 197

else

198
CFLAGS += $(CFLAGS_PARTS)
199 200

$(LIBRARY): $(OBJECTS)
201
	$(LD) $(LD_SHARED) $(LDFLAGS) $(TARGET_ARCH) $(LIBDIRS) $(OBJECTS) $(LIBS) $(OUTPUT_OPTION)
202 203 204

$(BUILD_DIR)/%.o: %.c
	$(CC) $(CFLAGS) $(TARGET_ARCH) -c $? $(OUTPUT_OPTION)
205 206 207 208 209 210 211 212 213

%.d: %.c
	@$(CC) -MM $< | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' > $@

-include $(SRCDEPS)

endif


214 215
# Cleanup targets

216
.PHONY: clean
217 218
clean:
	-rm -f $(OBJECTS)
219
	-rm -f $(SRCDEPS)
220

221
.PHONY: cleanall
222
cleanall: clean
223
	-rm -rf $(BUILD_DIR)
224

225
.PHONY: pristine
226
pristine: cleanall
227
	-rm -f $(GENSRCS)