-
Notifications
You must be signed in to change notification settings - Fork 0
/
JarHelper.cmake
329 lines (291 loc) · 14.6 KB
/
JarHelper.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# This helps CMake create an executable JAR file.
# The created JAR references external JAR libraries.
# The paths to the external JARs are included in the MANIFEST file.
function(create_jar _TARGET_NAME)
set(_JAVA_SOURCE_FILES ${ARGN})
#message("Compiling on OS: " ${CMAKE_HOST_SYSTEM_NAME})
#message("CMAKE_HOST_SYSTEM_VERSION "${CMAKE_HOST_SYSTEM_VERSION})
#message("CMAKE_HOST_SYSTEM_PROCESSOR: " ${CMAKE_HOST_SYSTEM_PROCESSOR})
#message("CMAKE_HOST_SYSTEM" ${CMAKE_HOST_SYSTEM})
#message("PROCESSOR_ARCHITEW6432: " $ENV{PROCESSOR_ARCHITEW6432})
#message("PROCESSOR_ARCHITECTURE: " $ENV{PROCESSOR_ARCHITECTURE})
if("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "Windows")
set(COPASI_LIBRARY_NAME "CopasiJava.dll")
set(LIBSBML_LIBRARY_NAME "sbmlj.dll")
message("Windows detected!!")
set(COPASI_DIR_OS "win")
set(LIBSBML_DIR_OS "win")
if($ENV{PROCESSOR_ARCHITEW6432} MATCHES "64")
message("64!!")
set(COPASI_DIR_ARCH 64)
set(LIBSBML_DIR_ARCH 64)
elseif($ENV{PROCESSOR_ARCHITEW6432} MATCHES "64")
message("32!!")
set(COPASI_DIR_ARCH 32)
set(LIBSBML_DIR_ARCH 32)
endif( $ENV{PROCESSOR_ARCHITEW6432} MATCHES "64")
endif("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "Windows")
if("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "Linux")
set(COPASI_LIBRARY_NAME "libCopasiJava.so")
set(LIBSBML_LIBRARY_NAME "libsbmlj.so")
message("Linux detected!!")
set(COPASI_DIR_OS "linux")
set(LIBSBML_DIR_OS "linux")
if("${CMAKE_HOST_SYSTEM_PROCESSOR}" MATCHES "64")
message("64!!")
set(COPASI_DIR_ARCH 64)
set(LIBSBML_DIR_ARCH 64)
else ("${CMAKE_HOST_SYSTEM_PROCESSOR}" MATCHES "64")
message("32!!")
set(COPASI_DIR_ARCH 32)
set(LIBSBML_DIR_ARCH 32)
endif("${CMAKE_HOST_SYSTEM_PROCESSOR}" MATCHES "64")
endif("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "Linux")
if("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "Darwin")
message("Darwin detected!!")
set(COPASI_DIR_OS "mac_universal")
set(LIBSBML_DIR_OS "mac_universal")
endif("${CMAKE_HOST_SYSTEM_NAME}" MATCHES "Darwin")
if (NOT DEFINED CMAKE_JAVA_TARGET_OUTPUT_DIR)
set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif(NOT DEFINED CMAKE_JAVA_TARGET_OUTPUT_DIR)
#get the location of the msmb project
ExternalProject_Get_Property(MSMB binary_dir)
#message(STATUS "MSMB location: " ${binary_dir})
#copy the COPASI library into the lib directory
file(COPY ${JCMC_SOURCE_DIR}/lib/MSMB/CopasiLibs/${COPASI_DIR_OS}${COPASI_DIR_ARCH}/${COPASI_LIBRARY_NAME} DESTINATION
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/lib)
file(COPY ${JCMC_SOURCE_DIR}/lib/MSMB/CopasiLibs/${COPASI_DIR_OS}${COPASI_DIR_ARCH}/copasi.jar DESTINATION
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/lib)
#copy the libSBML library into the lib directory
file(COPY ${JCMC_SOURCE_DIR}/lib/libSBML/${LIBSBML_DIR_OS}${LIBSBML_DIR_ARCH}/${LIBSBML_LIBRARY_NAME} DESTINATION
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/lib)
file(COPY ${JCMC_SOURCE_DIR}/lib/libSBML/${LIBSBML_DIR_OS}${LIBSBML_DIR_ARCH}/libsbmlj.jar DESTINATION
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/lib)
#copy the jgraph library into the lib directory
file(COPY ${JCMC_SOURCE_DIR}/lib/jgraphx.jar DESTINATION
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/lib)
file(GLOB MY_JARS "${CMAKE_JAVA_TARGET_OUTPUT_DIR}/lib/*.jar")
#add the MSMB jar to the java path
set(CMAKE_JAVA_INCLUDE_PATH ${MY_JARS}
${binary_dir}/MSMB.jar
)
#message("Java_Include_Path: " ${CMAKE_JAVA_INCLUDE_PATH})
if (CMAKE_JAVA_JAR_ENTRY_POINT)
set(_ENTRY_POINT_OPTION e)
set(_ENTRY_POINT_VALUE ${CMAKE_JAVA_JAR_ENTRY_POINT})
endif (CMAKE_JAVA_JAR_ENTRY_POINT)
if (LIBRARY_OUTPUT_PATH)
set(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH})
else (LIBRARY_OUTPUT_PATH)
set(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${CMAKE_JAVA_TARGET_OUTPUT_DIR})
endif (LIBRARY_OUTPUT_PATH)
set(CMAKE_JAVA_INCLUDE_PATH
${CMAKE_JAVA_INCLUDE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_JAVA_OBJECT_OUTPUT_PATH}
${CMAKE_JAVA_LIBRARY_OUTPUT_PATH}
)
if (WIN32 AND NOT CYGWIN)
set(CMAKE_JAVA_INCLUDE_FLAG_SEP ";")
else (WIN32 AND NOT CYGWIN)
set(CMAKE_JAVA_INCLUDE_FLAG_SEP ":")
endif(WIN32 AND NOT CYGWIN)
foreach (JAVA_INCLUDE_DIR ${CMAKE_JAVA_INCLUDE_PATH})
set(CMAKE_JAVA_INCLUDE_PATH_FINAL "${CMAKE_JAVA_INCLUDE_PATH_FINAL}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${JAVA_INCLUDE_DIR}")
endforeach(JAVA_INCLUDE_DIR)
set(CMAKE_JAVA_CLASS_OUTPUT_PATH "${CMAKE_JAVA_TARGET_OUTPUT_DIR}${CMAKE_FILES_DIRECTORY}/${_TARGET_NAME}.dir")
set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}.jar")
if (CMAKE_JAVA_TARGET_OUTPUT_NAME AND CMAKE_JAVA_TARGET_VERSION)
set(_JAVA_TARGET_OUTPUT_NAME "${CMAKE_JAVA_TARGET_OUTPUT_NAME}-${CMAKE_JAVA_TARGET_VERSION}.jar")
set(_JAVA_TARGET_OUTPUT_LINK "${CMAKE_JAVA_TARGET_OUTPUT_NAME}.jar")
elseif (CMAKE_JAVA_TARGET_VERSION)
set(_JAVA_TARGET_OUTPUT_NAME "${_TARGET_NAME}-${CMAKE_JAVA_TARGET_VERSION}.jar")
set(_JAVA_TARGET_OUTPUT_LINK "${_TARGET_NAME}.jar")
elseif (CMAKE_JAVA_TARGET_OUTPUT_NAME)
set(_JAVA_TARGET_OUTPUT_NAME "${CMAKE_JAVA_TARGET_OUTPUT_NAME}.jar")
endif (CMAKE_JAVA_TARGET_OUTPUT_NAME AND CMAKE_JAVA_TARGET_VERSION)
# reset
set(CMAKE_JAVA_TARGET_OUTPUT_NAME)
set(_JAVA_CLASS_FILES)
set(_JAVA_COMPILE_FILES)
set(_JAVA_DEPENDS)
set(_JAVA_RESOURCE_FILES)
foreach(_JAVA_SOURCE_FILE ${_JAVA_SOURCE_FILES})
get_filename_component(_JAVA_EXT ${_JAVA_SOURCE_FILE} EXT)
get_filename_component(_JAVA_FILE ${_JAVA_SOURCE_FILE} NAME_WE)
get_filename_component(_JAVA_PATH ${_JAVA_SOURCE_FILE} PATH)
get_filename_component(_JAVA_FULL ${_JAVA_SOURCE_FILE} ABSOLUTE)
file(RELATIVE_PATH _JAVA_REL_BINARY_PATH ${CMAKE_JAVA_TARGET_OUTPUT_DIR} ${_JAVA_FULL})
file(RELATIVE_PATH _JAVA_REL_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${_JAVA_FULL})
string(LENGTH ${_JAVA_REL_BINARY_PATH} _BIN_LEN)
string(LENGTH ${_JAVA_REL_SOURCE_PATH} _SRC_LEN)
if (${_BIN_LEN} LESS ${_SRC_LEN})
set(_JAVA_REL_PATH ${_JAVA_REL_BINARY_PATH})
else (${_BIN_LEN} LESS ${_SRC_LEN})
set(_JAVA_REL_PATH ${_JAVA_REL_SOURCE_PATH})
endif (${_BIN_LEN} LESS ${_SRC_LEN})
get_filename_component(_JAVA_REL_PATH ${_JAVA_REL_PATH} PATH)
if (_JAVA_EXT MATCHES ".java")
list(APPEND _JAVA_COMPILE_FILES ${_JAVA_SOURCE_FILE})
set(_JAVA_CLASS_FILE "${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_REL_PATH}/${_JAVA_FILE}.class")
set(_JAVA_CLASS_FILES ${_JAVA_CLASS_FILES} ${_JAVA_CLASS_FILE})
elseif (_JAVA_EXT MATCHES ".jar"
OR _JAVA_EXT MATCHES ".war"
OR _JAVA_EXT MATCHES ".ear"
OR _JAVA_EXT MATCHES ".sar")
list(APPEND CMAKE_JAVA_INCLUDE_PATH ${_JAVA_SOURCE_FILE})
elseif (_JAVA_EXT STREQUAL "")
list(APPEND CMAKE_JAVA_INCLUDE_PATH ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}} ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}_CLASSPATH})
list(APPEND _JAVA_DEPENDS ${JAVA_JAR_TARGET_${_JAVA_SOURCE_FILE}})
else (_JAVA_EXT MATCHES ".java")
__java_copy_file(${CMAKE_CURRENT_SOURCE_DIR}/${_JAVA_SOURCE_FILE}
${CMAKE_JAVA_CLASS_OUTPUT_PATH}/${_JAVA_SOURCE_FILE}
"Copying ${_JAVA_SOURCE_FILE} to the build directory")
list(APPEND _JAVA_RESOURCE_FILES ${_JAVA_SOURCE_FILE})
endif (_JAVA_EXT MATCHES ".java")
endforeach(_JAVA_SOURCE_FILE)
#Copy the version file to the OUTPUT path
file(COPY ${JCMC_SOURCE_DIR}/version.txt DESTINATION
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/util/)
#Copy the logo icon to the OUTPUT path
file(COPY ${JCMC_SOURCE_DIR}/util/logo.png DESTINATION
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/util/)
# create an empty java_class_filelist
if (NOT EXISTS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist)
file(WRITE ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist "")
endif()
if (_JAVA_COMPILE_FILES)
# Compile the java files and create a list of class files
add_custom_command(
# NOTE: this command generates an artificial dependency file
OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
COMMAND ${Java_JAVAC_EXECUTABLE}
${CMAKE_JAVA_COMPILE_FLAGS}
-classpath "${CMAKE_JAVA_INCLUDE_PATH_FINAL}"
-d ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
${_JAVA_COMPILE_FILES}
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
DEPENDS ${_JAVA_COMPILE_FILES}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Building Java objects for ${_TARGET_NAME}.jar"
)
add_custom_command(
OUTPUT ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
COMMAND ${CMAKE_COMMAND}
-DCMAKE_JAVA_CLASS_OUTPUT_PATH=${CMAKE_JAVA_CLASS_OUTPUT_PATH}
-DCMAKE_JAR_CLASSES_PREFIX="${CMAKE_JAR_CLASSES_PREFIX}"
-P ${_JAVA_CLASS_FILELIST_SCRIPT}
DEPENDS ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_compiled_${_TARGET_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif (_JAVA_COMPILE_FILES)
# create the manifest file for the jar file
set(ManifestInfo
"Manifest-Version: 1.0
Class-Path: . ./lib/jgraphx.jar ./lib/copasi.jar ./lib/libsbmlj.jar ./lib/MSMB/MSMB.jar
Main-Class: acgui.AC_GUI
"
)
set(_JAVA_JAR_MANIFEST_PATH ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/MANIFEST.MF)
file(WRITE ${_JAVA_JAR_MANIFEST_PATH} ${ManifestInfo})
# create the jar file
set(_JAVA_JAR_OUTPUT_PATH
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_NAME})
if (CMAKE_JNI_TARGET)
add_custom_command(
OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
COMMAND ${Java_JAR_EXECUTABLE}
-cf${_ENTRY_POINT_OPTION} ${_JAVA_JAR_OUTPUT_PATH} ${_ENTRY_POINT_VALUE}
${_JAVA_RESOURCE_FILES} @java_class_filelist
COMMAND ${CMAKE_COMMAND}
-D_JAVA_TARGET_DIR=${CMAKE_JAVA_TARGET_OUTPUT_DIR}
-D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
-D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
-P ${_JAVA_SYMLINK_SCRIPT}
COMMAND ${CMAKE_COMMAND}
-D_JAVA_TARGET_DIR=${CMAKE_JAVA_TARGET_OUTPUT_DIR}
-D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_JAR_OUTPUT_PATH}
-D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
-P ${_JAVA_SYMLINK_SCRIPT}
DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
)
else ()
add_custom_command(
OUTPUT ${_JAVA_JAR_OUTPUT_PATH}
COMMAND ${Java_JAR_EXECUTABLE}
-cfm ${_JAVA_JAR_OUTPUT_PATH} ${_JAVA_JAR_MANIFEST_PATH}
${_JAVA_RESOURCE_FILES} @java_class_filelist
COMMAND ${CMAKE_COMMAND}
-D_JAVA_TARGET_DIR=${CMAKE_JAVA_TARGET_OUTPUT_DIR}
-D_JAVA_TARGET_OUTPUT_NAME=${_JAVA_TARGET_OUTPUT_NAME}
-D_JAVA_TARGET_OUTPUT_LINK=${_JAVA_TARGET_OUTPUT_LINK}
-P ${_JAVA_SYMLINK_SCRIPT}
WORKING_DIRECTORY ${CMAKE_JAVA_CLASS_OUTPUT_PATH}
DEPENDS ${_JAVA_RESOURCE_FILES} ${_JAVA_DEPENDS} ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/java_class_filelist
COMMENT "Creating Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
)
endif (CMAKE_JNI_TARGET)
# Add the target and make sure we have the latest resource files.
add_custom_target(${_TARGET_NAME} ALL DEPENDS ${_JAVA_JAR_OUTPUT_PATH})
set_property(
TARGET
${_TARGET_NAME}
PROPERTY
INSTALL_FILES
${_JAVA_JAR_OUTPUT_PATH}
)
if (_JAVA_TARGET_OUTPUT_LINK)
set_property(
TARGET
${_TARGET_NAME}
PROPERTY
INSTALL_FILES
${_JAVA_JAR_OUTPUT_PATH}
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
)
if (CMAKE_JNI_TARGET)
set_property(
TARGET
${_TARGET_NAME}
PROPERTY
JNI_SYMLINK
${CMAKE_JAVA_TARGET_OUTPUT_DIR}/${_JAVA_TARGET_OUTPUT_LINK}
)
endif (CMAKE_JNI_TARGET)
endif (_JAVA_TARGET_OUTPUT_LINK)
set_property(
TARGET
${_TARGET_NAME}
PROPERTY
JAR_FILE
${_JAVA_JAR_OUTPUT_PATH}
)
set_property(
TARGET
${_TARGET_NAME}
PROPERTY
CLASSDIR
${CMAKE_JAVA_CLASS_OUTPUT_PATH}
)
# on linux and MAC, for unknown reasons, the addLibrary from inside the MSMB does not seem to work
# so we need to explicitly set the library path to the libs folder.
# We created a "launcher" script that set the path according to the compilation setup.
# If the final directory is moved, it is the user responsibility to change the parameter in the script accordingly.
if(${COPASI_DIR_OS} MATCHES "linux")
execute_process(COMMAND echo "export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH \njava -jar" ${_TARGET_NAME}.jar
WORKING_DIRECTORY ${CMAKE_JAVA_TARGET_OUTPUT_DIR}
OUTPUT_FILE "${_TARGET_NAME}_launcher.sh")
endif(${COPASI_DIR_OS} MATCHES "linux")
if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
#Copy the main script to reset the working directory of the launcher file
file(COPY ${JCMC_SOURCE_DIR}/MSMB/CopasiLibs/${COPASI_DIR_OS}${COPASI_DIR_ARCH}/scriptToRunJar.txt
DESTINATION ${CMAKE_JAVA_TARGET_OUTPUT_DIR}
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ)
file(RENAME ${CMAKE_JAVA_TARGET_OUTPUT_DIR}/scriptToRunJar.txt ${CMAKE_JAVA_TARGET_OUTPUT_DIR}/${_TARGET_NAME}_launcher.command )
file(APPEND ${CMAKE_JAVA_TARGET_OUTPUT_DIR}/${_TARGET_NAME}_launcher.command "\njava -Djava.library.path=./lib -jar ${_TARGET_NAME}.jar")
endif(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
add_dependencies(JCMC MSMB)
endfunction(create_jar)