Makefile 5 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

Matteo Basso's avatar
Matteo Basso committed
81
    ifeq (,$(filter $(TARGET_CPU),x86_64 ppc64))
82
83
84
        CFLAGS += -m32
        LDFLAGS += -melf_i386
    endif
85
86
87
88
89
else
    $(error Target environment $(TARGET_ENV) is not supported)
endif


90
91
ifeq (x86_64,$(TARGET_CPU))
    BUILD_DIR := $(JDK_TARGET)-x86_64
Matteo Basso's avatar
Matteo Basso committed
92
93
else ifeq (ppc64,$(TARGET_CPU))
    BUILD_DIR := $(JDK_TARGET)-ppc64
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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)


110
111
# Source and object files needed to create the library
SOURCES = bytecode.c common.c jvmtiutil.c connection.c \
112
	connpool.c msgchannel.c network.c classparser.c \
113
114
115
116
117
	dislserver.pb-c.c protobuf-c.c dislagent.c

HEADERS = $(wildcard *.h) codeflags.h dislserver.pb-c.h
GENSRCS = bytecode.c codeflags.h \
	dislserver.pb-c.c dislserver.pb-c.h
118

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


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

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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))

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


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


159
# Default goal
160

161
.PHONY: agent
162
agent: $(GENSRCS) $(BUILD_DIR) $(LIBRARY)
163
164


165
# Convenience targets
166

167
168
169
.PHONY: whole
whole:
	@$(MAKE) WHOLE=1
170

171
172
173
.PHONY: debug
debug:
	@$(MAKE) DEBUG=1
174

175

176
177
# Generated files

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

181
182
183
184
185

# Output directory for compiled object files

$(BUILD_DIR):
	mkdir $@
186
187


188
189
190
191
# Compilation and linking targets

ifneq (,$(WHOLE))

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

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

else

199
CFLAGS += $(CFLAGS_PARTS)
200
201

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

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

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

-include $(SRCDEPS)

endif


215
216
# Cleanup targets

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

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

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