Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add context ucontext for windows.
[simgrid.git] / buildtools / Cmake / CompleteInFiles.cmake
1 include(CheckFunctionExists)
2 include(CheckIncludeFile)
3 include(CheckIncludeFiles)
4 include(CheckLibraryExists)
5
6 # Checks for header libraries functions.
7
8 CHECK_LIBRARY_EXISTS(pthread    pthread_create          NO_DEFAULT_PATHS pthread)
9 CHECK_LIBRARY_EXISTS(pthread    sem_init                NO_DEFAULT_PATHS HAVE_SEM_INIT_LIB)
10 CHECK_LIBRARY_EXISTS(pthread    sem_timedwait           NO_DEFAULT_PATHS HAVE_SEM_TIMEDWAIT_LIB)
11 CHECK_LIBRARY_EXISTS(pthread    pthread_mutex_timedlock NO_DEFAULT_PATHS HAVE_MUTEX_TIMEDLOCK_LIB)
12 CHECK_LIBRARY_EXISTS(rt         clock_gettime           NO_DEFAULT_PATHS HAVE_POSIX_GETTIME)
13
14 CHECK_INCLUDE_FILES("time.h;sys/time.h" TIME_WITH_SYS_TIME)
15 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
16 CHECK_INCLUDE_FILE(pthread.h HAVE_PTHREAD_H)
17 CHECK_INCLUDE_FILE(valgrind/valgrind.h HAVE_VALGRIND_VALGRIND_H)
18 CHECK_INCLUDE_FILE(socket.h HAVE_SOCKET_H)
19 CHECK_INCLUDE_FILE(sys/socket.h HAVE_SYS_SOCKET_H)
20 CHECK_INCLUDE_FILE(stat.h HAVE_STAT_H)
21 CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
22 CHECK_INCLUDE_FILE(windows.h HAVE_WINDOWS_H)
23 CHECK_INCLUDE_FILE(winsock.h HAVE_WINSOCK_H)
24 CHECK_INCLUDE_FILE(winsock2.h HAVE_WINSOCK2_H)
25 CHECK_INCLUDE_FILE(errno.h HAVE_ERRNO_H)
26 CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
27 CHECK_INCLUDE_FILE(execinfo.h HAVE_EXECINFO_H)
28 CHECK_INCLUDE_FILE(signal.h HAVE_SIGNAL_H)
29 CHECK_INCLUDE_FILE(sys/time.h HAVE_SYS_TIME_H)
30 CHECK_INCLUDE_FILE(time.h HAVE_TIME_H)
31 CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
32 CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
33 CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
34 CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
35 CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
36 CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
37 CHECK_INCLUDE_FILE(ucontext.h HAVE_UCONTEXT_H)
38
39 CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
40 CHECK_FUNCTION_EXISTS(usleep HAVE_USLEEP)
41 CHECK_FUNCTION_EXISTS(getdtablesize HAVE_GETDTABLESIZE)
42 CHECK_FUNCTION_EXISTS(sysconf HAVE_SYSCONF)
43 CHECK_FUNCTION_EXISTS(readv HAVE_READV)
44 CHECK_FUNCTION_EXISTS(popen HAVE_POPEN)
45 CHECK_FUNCTION_EXISTS(signal HAVE_SIGNAL)
46 CHECK_FUNCTION_EXISTS(snprintf HAVE_SNPRINTF)
47 CHECK_FUNCTION_EXISTS(vsnprintf HAVE_VSNPRINTF)
48 CHECK_FUNCTION_EXISTS(asprintf HAVE_ASPRINTF)
49 CHECK_FUNCTION_EXISTS(vasprintf HAVE_VASPRINTF)
50 CHECK_FUNCTION_EXISTS(makecontext HAVE_MAKECONTEXT)
51 CHECK_FUNCTION_EXISTS(mmap HAVE_MMAP)
52
53 if(WIN32)
54     set(HAVE_UCONTEXT_H 1)
55     set(HAVE_MAKECONTEXT 1)
56 endif(WIN32)
57
58 set(CONTEXT_UCONTEXT 0)
59 SET(CONTEXT_THREADS 0)
60 SET(HAVE_RUBY 0)
61 set(HAVE_LUA 0)
62 SET(HAVE_JAVA 0)
63 SET(HAVE_TRACING 0)
64
65 if(enable_tracing)
66         SET(HAVE_TRACING 1)
67 endif(enable_tracing)
68
69 if(enable_model-checking AND HAVE_MMAP)
70         SET(HAVE_MC 1)
71         SET(MMALLOC_WANT_OVERIDE_LEGACY 1)
72 else(enable_model-checking AND HAVE_MMAP)
73         SET(HAVE_MC 0)
74         SET(MMALLOC_WANT_OVERIDE_LEGACY 0)
75 endif(enable_model-checking AND HAVE_MMAP)
76
77 if(enable_lua)
78         exec_program("lua -v" OUTPUT_VARIABLE LUA_VERSION)
79         string(REGEX MATCH "[0-9].[0-9].[0-9]" LUA_VERSION "${LUA_VERSION}")
80         string(REGEX MATCH "^[0-9]" LUA_MAJOR_VERSION "${LUA_VERSION}")
81         string(REPLACE "${LUA_MAJOR_VERSION}." "" LUA_VERSION "${LUA_VERSION}")
82         string(REGEX MATCH "^[0-9]" LUA_MINOR_VERSION "${LUA_VERSION}")
83         string(REPLACE "${LUA_MINOR_VERSION}." "" LUA_PATCH_VERSION "${LUA_VERSION}")
84
85         if(LUA_MAJOR_VERSION MATCHES "5" AND LUA_MINOR_VERSION MATCHES "1")
86         
87                 find_path(HAVE_LUA5_1_LUALIB_H
88                 NAMES lualib.h 
89                 PATHS "/sw/include/" "/usr/include/lua${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}/"
90                 )
91                 find_path(HAVE_LUA5_1_LAUXLIB_H
92                 NAMES lauxlib.h
93                 PATHS "/sw/include/" "/usr/include/lua${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}/"
94                 )
95                 find_library(LUA_LIB_PATH_1
96                 NAMES lua${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}
97                 PATHS /usr
98                 )
99                 find_library(LUA_LIB_PATH_2
100                 NAMES lua-${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}
101                 PATHS /usr
102                 )
103                 find_library(LUA_LIB_PATH_3
104                 NAMES lua.${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}.${LUA_PATCH_VERSION}
105                 PATHS /sw
106                 )
107         
108                 if(HAVE_LUA5_1_LUALIB_H AND HAVE_LUA5_1_LAUXLIB_H)
109                         set(HAVE_LUA 1)
110                         SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-I${HAVE_LUA5_1_LUALIB_H} ")
111         
112                         if(NOT HAVE_LUA5_1_LUALIB_H STREQUAL HAVE_LUA5_1_LAUXLIB_H)
113                                 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-I${HAVE_LUA5_1_LAUXLIB_H} ")
114                                 #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}-I${HAVE_LUA5_1_LAUXLIB_H} ")
115                         endif(NOT HAVE_LUA5_1_LUALIB_H STREQUAL HAVE_LUA5_1_LAUXLIB_H)
116                 endif(HAVE_LUA5_1_LUALIB_H AND HAVE_LUA5_1_LAUXLIB_H)
117         
118                 if(LUA_LIB_PATH_1)
119                         set(liblua lua${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION})
120                         set(lua_lib_path_to_use ${LUA_LIB_PATH_1})
121                 endif(LUA_LIB_PATH_1)
122         
123                 if(LUA_LIB_PATH_2)
124                         set(liblua lua-${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION})
125                         set(lua_lib_path_to_use ${LUA_LIB_PATH_2})
126                 endif(LUA_LIB_PATH_2)
127         
128                 if(LUA_LIB_PATH_3)
129                         set(liblua lua.${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}.${LUA_PATCH_VERSION})
130                         set(lua_lib_path_to_use ${LUA_LIB_PATH_3})
131                 endif(LUA_LIB_PATH_3)
132         
133                 if(NOT LUA_LIB_PATH_1 AND NOT LUA_LIB_PATH_2 AND NOT LUA_LIB_PATH_3)
134                         set(HAVE_LUA 0)
135                 else(NOT LUA_LIB_PATH_1 AND NOT LUA_LIB_PATH_2 AND NOT LUA_LIB_PATH_3)
136                         string(REGEX REPLACE "liblua.*" "" lua_lib_path_to_use ${lua_lib_path_to_use})
137                         string(REGEX MATCH "-L${lua_lib_path_to_use}" operation "${CMAKE_EXE_LINKER_FLAGS}")
138                         if(NOT operation)
139                                 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}-L${lua_lib_path_to_use} ")
140                         endif(NOT operation)
141                 endif(NOT LUA_LIB_PATH_1 AND NOT LUA_LIB_PATH_2 AND NOT LUA_LIB_PATH_3)
142                 
143         else(LUA_MAJOR_VERSION MATCHES "5" AND LUA_MINOR_VERSION MATCHES "1")
144                 message("Lua binding need version 5.1.x actually version ${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}.x")
145         endif(LUA_MAJOR_VERSION MATCHES "5" AND LUA_MINOR_VERSION MATCHES "1")
146         
147 endif(enable_lua)
148
149 if(enable_ruby)
150         include(FindRuby)
151         if(RUBY_LIBRARY)
152                 if(RUBY_VERSION_MAJOR MATCHES "1" AND RUBY_VERSION_MINOR MATCHES "8")
153                         set(LIB_RUBY_VERSION "${RUBY_VERSION_MAJOR}.${RUBY_VERSION_MINOR}.${RUBY_VERSION_PATCH}")
154                         string(REGEX MATCH "ruby.*[0-9]" RUBY_LIBRARY_NAME ${RUBY_LIBRARY})
155                         if(NOT RUBY_LIBRARY_NAME)
156                                 set(RUBY_LIBRARY_NAME ruby)
157                         endif(NOT RUBY_LIBRARY_NAME)
158                         string(REGEX REPLACE "libruby.*$" "" RUBY_LIBRARY ${RUBY_LIBRARY})
159                         SET(CMAKE_EXE_LINKER_FLAGS "-L${RUBY_LIBRARY}")
160                         SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-I${RUBY_CONFIG_INCLUDE_DIR} ") #path to config.h
161                         string(COMPARE EQUAL "${RUBY_INCLUDE_DIR}" "${RUBY_CONFIG_INCLUDE_DIR}" operation)
162                         if(NOT operation)
163                                 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-I${RUBY_INCLUDE_DIR} ") #path to ruby.h
164                         endif(NOT operation)
165                         ADD_DEFINITIONS("-I${PROJECT_DIRECTORY}/src/bindings/ruby -I${PROJECT_DIRECTORY}/src/simix")
166                         SET(HAVE_RUBY 1)
167                 else(RUBY_VERSION_MAJOR MATCHES "1" AND RUBY_VERSION_MINOR MATCHES "8")
168                         message("Ruby binding need version 1.8.x actually version ${RUBY_VERSION_MAJOR}.${RUBY_VERSION_MINOR}.x")
169                         SET(HAVE_RUBY 0)
170                 endif(RUBY_VERSION_MAJOR MATCHES "1" AND RUBY_VERSION_MINOR MATCHES "8")
171         else(RUBY_LIBRARY)
172                 SET(HAVE_RUBY 0)
173         endif(RUBY_LIBRARY)
174         
175         if(NOT RUBY_EXECUTABLE)
176                 message("Take care : you don't have ruby executable so you can compile and build examples but can't execute them!!!") 
177         endif(NOT RUBY_EXECUTABLE)
178         
179 endif(enable_ruby)
180
181 #--------------------------------------------------------------------------------------------------
182 ### Initialize of CONTEXT JAVA
183
184 if(enable_java)
185         include(FindJava)
186         include(FindJNI)
187         if(JAVA_INCLUDE_PATH)
188                 set(HAVE_JNI_H 1)
189         endif(JAVA_INCLUDE_PATH)        
190         if(JAVA_COMPILE AND JAVA_INCLUDE_PATH AND JAVA_INCLUDE_PATH2)
191                 SET(HAVE_JAVA 1)
192                 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-I${JAVA_INCLUDE_PATH} ")
193                 if(NOT JAVA_INCLUDE_PATH STREQUAL JAVA_INCLUDE_PATH2)
194                 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-I${JAVA_INCLUDE_PATH2} ")                   
195                 endif(NOT JAVA_INCLUDE_PATH STREQUAL JAVA_INCLUDE_PATH2)
196         else(JAVA_COMPILE AND JAVA_INCLUDE_PATH AND JAVA_INCLUDE_PATH2) 
197                 SET(HAVE_JAVA 0)
198         endif(JAVA_COMPILE AND JAVA_INCLUDE_PATH AND JAVA_INCLUDE_PATH2)
199 endif(enable_java)
200
201 #--------------------------------------------------------------------------------------------------
202 ### Initialize of CONTEXT GTNETS
203 if(NOT enable_gtnets OR enable_supernovae)
204         SET(HAVE_GTNETS 0)
205 else(NOT enable_gtnets OR enable_supernovae)
206         set(GTNETS_LDFLAGS "-L${gtnets_path}/lib")
207         set(GTNETS_CPPFLAGS "-I${gtnets_path}/include/gtnets")
208         exec_program("${CMAKE_CXX_COMPILER} ${GTNETS_CPPFLAGS} -lgtnets ${GTNETS_LDFLAGS} ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_gtnets.cpp " OUTPUT_VARIABLE COMPILE_GTNETS_VAR) 
209         if(COMPILE_GTNETS_VAR)
210                 SET(HAVE_GTNETS 0)
211         else(COMPILE_GTNETS_VAR)
212                 SET(HAVE_GTNETS 1)
213                 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}${GTNETS_LDFLAGS} ${GTNETS_CPPFLAGS} ")
214                 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${GTNETS_LDFLAGS} ${GTNETS_CPPFLAGS} ")
215         endif(COMPILE_GTNETS_VAR)
216 endif(NOT enable_gtnets OR enable_supernovae)
217
218 #--------------------------------------------------------------------------------------------------
219 ### Initialize of CONTEXT THREADS
220
221 if(pthread)
222         set(pthread 1)
223 elseif(pthread)
224         set(pthread 0)
225 endif(pthread)
226
227 if(pthread)
228         ### HAVE_SEM_INIT
229         
230         if(HAVE_SEM_INIT_LIB)
231                 exec_program("${CMAKE_C_COMPILER} -lpthread ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_sem_init.c" OUTPUT_VARIABLE HAVE_SEM_INIT_run)
232                 if(HAVE_SEM_INIT_run)
233                         set(HAVE_SEM_INIT 0)
234                 else(HAVE_SEM_INIT_run)
235                         set(HAVE_SEM_INIT 1)
236                 endif(HAVE_SEM_INIT_run)
237         endif(HAVE_SEM_INIT_LIB)
238
239         ### HAVE_SEM_TIMEDWAIT
240
241         if(HAVE_SEM_TIMEDWAIT_LIB)
242                 exec_program("${CMAKE_C_COMPILER} -lpthread ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_sem_timedwait.c" OUTPUT_VARIABLE HAVE_SEM_TIMEDWAIT_run)
243                 if(HAVE_SEM_TIMEDWAIT_run)
244                         set(HAVE_SEM_TIMEDWAIT 0)
245                 else(HAVE_SEM_TIMEDWAIT_run)
246                         set(HAVE_SEM_TIMEDWAIT 1)
247                 endif(HAVE_SEM_TIMEDWAIT_run)
248         endif(HAVE_SEM_TIMEDWAIT_LIB)
249
250         ### HAVE_MUTEX_TIMEDLOCK
251
252         if(HAVE_MUTEX_TIMEDLOCK_LIB)
253                 exec_program("${CMAKE_C_COMPILER} -lpthread ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_mutex_timedlock.c" OUTPUT_VARIABLE HAVE_SEM_TIMEDWAIT_run)
254                 if(HAVE_MUTEX_TIMEDLOCK_run)
255                         set(HAVE_MUTEX_TIMEDLOCK 0)
256                 else(HAVE_MUTEX_TIMEDLOCK_run)
257                         set(HAVE_MUTEX_TIMEDLOCK 1)
258                 endif(HAVE_MUTEX_TIMEDLOCK_run)
259         endif(HAVE_MUTEX_TIMEDLOCK_LIB)
260 endif(pthread)
261
262 # AC_CHECK_MCSC(mcsc=yes, mcsc=no) 
263 set(mcsc_flags "")
264 if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
265         set(mcsc_flags "-D_XOPEN_SOURCE")
266 endif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
267
268 try_run(RUN_mcsc_VAR COMPILE_mcsc_VAR
269         ${PROJECT_DIRECTORY}
270         ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_AC_CHECK_MCSC.c
271         COMPILE_DEFINITIONS "${mcsc_flags}"
272         )
273         
274 if(EXISTS "${simgrid_BINARY_DIR}/conftestval")
275         file(READ "${simgrid_BINARY_DIR}/conftestval" mcsc)
276         STRING(REPLACE "\n" "" mcsc "${mcsc}")
277         if(mcsc)
278                 set(mcsc "yes")
279         elseif(mcsc)
280                 set(mcsc "no")
281         endif(mcsc)
282 else(EXISTS "${simgrid_BINARY_DIR}/conftestval")
283         set(mcsc "no")
284 endif(EXISTS "${simgrid_BINARY_DIR}/conftestval")
285
286 if(mcsc MATCHES "no" AND pthread)
287         if(HAVE_WINDOWS_H)
288                 set(windows_context "yes")
289                 set(IS_WINDOWS 1)
290         elseif(HAVE_WINDOWS_H)
291                 message(FATAL_ERROR "no appropriate backend found")
292         endif(HAVE_WINDOWS_H)
293 endif(mcsc MATCHES "no" AND pthread)
294
295 if(with_context MATCHES "ucontext" AND mcsc MATCHES "no")
296         message(FATAL_ERROR "-Dwith-context=ucontext specified but ucontext unusable.")
297 endif(with_context MATCHES "ucontext" AND mcsc MATCHES "no")
298
299 set(with_context_ok 0)
300 if(with_context MATCHES "windows")
301         set(with_context_ok 1)
302         if(NOT HAVE_WINDOWS_H)
303                 message(FATAL_ERROR "no appropriate backend found windows")
304         endif(NOT HAVE_WINDOWS_H)
305 endif(with_context MATCHES "windows")
306
307 if(with_context MATCHES "pthreads")
308         set(with_context_ok 1)
309         set(with_context "pthread")
310 endif(with_context MATCHES "pthreads")
311
312 if(with_context MATCHES "auto")
313         set(with_context_ok 1)
314         set(with_context "ucontext")
315         message("with_context auto change to ucontext")
316 endif(with_context MATCHES "auto")
317
318 if(with_context MATCHES "ucontext")
319         set(with_context_ok 1)
320         if(mcsc)
321                 set(CONTEXT_UCONTEXT 1)
322         else(mcsc)
323                 if(windows_context MATCHES "yes")
324                         set(with_context "windows")
325                         message("with_context ucontext change to windows")
326                 else(windows_context MATCHES "yes")
327                         set(with_context "pthread")
328                         message("with_context ucontext change to pthread")
329                 endif(windows_context MATCHES "yes")
330         endif(mcsc)
331 endif(with_context MATCHES "ucontext")
332
333 if(with_context MATCHES "pthread")
334         set(with_context_ok 1)
335         if(NOT pthread)
336                 message(FATAL_ERROR "Cannot find pthreads (try -Dwith_context=ucontext if you haven't already tried).")
337         endif(NOT pthread)
338         SET(CONTEXT_THREADS 1)
339 endif(with_context MATCHES "pthread")
340
341 if(with_context MATCHES "ucontext")
342         SET(CONTEXT_THREADS 0)
343 endif(with_context MATCHES "ucontext")
344
345 if(NOT with_context_ok)
346         message(FATAL_ERROR "-Dwith-context must be either ucontext or pthread")
347 endif(NOT with_context_ok)
348
349 ###############
350 ## SVN version check
351 ##
352 if(IS_DIRECTORY ${PROJECT_DIRECTORY}/.svn)
353         find_file(SVN ".svn" ${PROJECT_DIRECTORY})
354         exec_program("svnversion ${PROJECT_DIRECTORY}" OUTPUT_VARIABLE "SVN_VERSION")
355         message("SVN_VERSION : ${SVN_VERSION}")
356 endif(IS_DIRECTORY ${PROJECT_DIRECTORY}/.svn)
357
358 if(IS_DIRECTORY ${PROJECT_DIRECTORY}/.git)
359
360         exec_program("git --git-dir=${PROJECT_DIRECTORY}/.git log --oneline -1" OUTPUT_VARIABLE "GIT_VERSION")
361         exec_program("git --git-dir=${PROJECT_DIRECTORY}/.git log -n 1 --format=%ai ." OUTPUT_VARIABLE "GIT_DATE")
362         exec_program("git svn info" ${PROJECT_DIRECTORY} OUTPUT_VARIABLE "GIT_SVN_VERSION")
363         
364         string(REGEX REPLACE " .*" "" GIT_VERSION "${GIT_VERSION}")
365         string(REPLACE "\n" ";" GIT_SVN_VERSION ${GIT_SVN_VERSION})
366         STRING(REPLACE " +0000" "" GIT_DATE ${GIT_DATE})
367         STRING(REPLACE " " "~" GIT_DATE ${GIT_DATE})
368         STRING(REPLACE ":" "-" GIT_DATE ${GIT_DATE})
369         foreach(line ${GIT_SVN_VERSION})
370                 string(REGEX MATCH "^Revision:.*" line_good ${line})
371                 if(line_good)
372                         string(REPLACE "Revision: " "" line_good ${line_good})
373                         set(SVN_VERSION ${line_good})
374                 endif(line_good)
375         endforeach(line ${GIT_SVN_VERSION})
376 endif(IS_DIRECTORY ${PROJECT_DIRECTORY}/.git)
377
378 ###################################
379 ## SimGrid and GRAS specific checks
380 ##
381 include(TestBigEndian)
382 TEST_BIG_ENDIAN(BIGENDIAN)
383 # Check architecture signature begin
384 try_run(RUN_GRAS_VAR COMPILE_GRAS_VAR
385         ${PROJECT_DIRECTORY}
386         ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_GRAS_ARCH.c
387         RUN_OUTPUT_VARIABLE var1
388         )
389 if(BIGENDIAN)
390 set(val_big "B${var1}")
391 set(GRAS_BIGENDIAN 1)
392 else(BIGENDIAN)
393 set(val_big "l${var1}")
394 set(GRAS_BIGENDIAN 0)
395 endif(BIGENDIAN)
396
397 if(val_big MATCHES "l_C:1/1:_I:2/1:4/1:4/1:8/1:_P:4/1:4/1:_D:4/1:8/1:")
398         #gras_arch=0; gras_size=32; gras_arch_name=little32_1;
399         SET(GRAS_ARCH_32_BITS 1)
400         SET(GRAS_THISARCH 0)
401 endif(val_big MATCHES "l_C:1/1:_I:2/1:4/1:4/1:8/1:_P:4/1:4/1:_D:4/1:8/1:")
402 if(val_big MATCHES "l_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2:")
403         #gras_arch=1; gras_size=32; gras_arch_name=little32_2;
404         SET(GRAS_ARCH_32_BITS 1)
405         SET(GRAS_THISARCH 1)
406 endif(val_big MATCHES "l_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2:")
407 if(val_big MATCHES "l_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:") 
408         #gras_arch=2; gras_size=32; gras_arch_name=little32_4;
409         SET(GRAS_ARCH_32_BITS 1)
410         SET(GRAS_THISARCH 2)
411 endif(val_big MATCHES "l_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:")
412 if(val_big MATCHES "l_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:") 
413         #gras_arch=3; gras_size=32; gras_arch_name=little32_8;
414         SET(GRAS_ARCH_32_BITS 1)
415         SET(GRAS_THISARCH 3)
416 endif(val_big MATCHES "l_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:") 
417 if(val_big MATCHES "l_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:") 
418         #gras_arch=4; gras_size=64; gras_arch_name=little64;
419         SET(GRAS_ARCH_32_BITS 0)
420         SET(GRAS_THISARCH 4)
421 endif(val_big MATCHES "l_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:")
422
423 if(val_big MATCHES "B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:") 
424         #gras_arch=5; gras_size=32; gras_arch_name=big32;
425         SET(GRAS_ARCH_32_BITS 1)
426         SET(GRAS_THISARCH 5)
427 endif(val_big MATCHES "B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:")
428 if(val_big MATCHES "B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/4:") 
429         #gras_arch=6; gras_size=32; gras_arch_name=big32_8_4;
430         SET(GRAS_ARCH_32_BITS 1)
431         SET(GRAS_THISARCH 6)
432 endif(val_big MATCHES "B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/4:")
433 if(val_big MATCHES "B_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:") 
434         #gras_arch=7; gras_size=32; gras_arch_name=big32_4;
435         SET(GRAS_ARCH_32_BITS 1)
436         SET(GRAS_THISARCH 7)
437 endif(val_big MATCHES "B_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:")
438 if(val_big MATCHES "B_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2:") 
439         #gras_arch=8; gras_size=32; gras_arch_name=big32_2;
440         SET(GRAS_ARCH_32_BITS 1)
441         SET(GRAS_THISARCH 8)
442 endif(val_big MATCHES "B_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2:") 
443 if(val_big MATCHES "B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:") 
444         #gras_arch=9; gras_size=64; gras_arch_name=big64;
445         SET(GRAS_ARCH_32_BITS 0)
446         SET(GRAS_THISARCH 9)
447 endif(val_big MATCHES "B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:")
448 if(val_big MATCHES "B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/4:") 
449         #gras_arch=10;gras_size=64; gras_arch_name=big64_8_4;
450         SET(GRAS_ARCH_32_BITS 0)
451         SET(GRAS_THISARCH 10)
452 endif(val_big MATCHES "B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/4:") 
453
454
455 # Check architecture signature end
456 try_run(RUN_GRAS_VAR COMPILE_GRAS_VAR
457         ${PROJECT_DIRECTORY}
458         ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_GRAS_CHECK_STRUCT_COMPACTION.c
459         RUN_OUTPUT_VARIABLE var2
460         )
461 separate_arguments(var2)
462 foreach(var_tmp ${var2})
463         set(${var_tmp} 1)
464 endforeach(var_tmp ${var2})
465
466 # Check for [SIZEOF_MAX]
467 try_run(RUN_SM_VAR COMPILE_SM_VAR
468         ${PROJECT_DIRECTORY}
469         ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_max_size.c
470         RUN_OUTPUT_VARIABLE var3
471         )
472 SET(SIZEOF_MAX ${var3})
473
474 #--------------------------------------------------------------------------------------------------
475
476 set(makecontext_CPPFLAGS_2 "")
477 if(HAVE_MAKECONTEXT OR WIN32)
478         set(makecontext_CPPFLAGS "-DTEST_makecontext")
479         if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
480                 set(makecontext_CPPFLAGS_2 "-DOSX")
481         endif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
482         
483         if(WIN32)
484             set(makecontext_CPPFLAGS_2 "-DWIN32 ${INCLUDES}")
485         endif(WIN32)
486
487         try_run(RUN_makecontext_VAR COMPILE_makecontext_VAR
488                 ${PROJECT_DIRECTORY}
489                 ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_stacksetup.c
490                 COMPILE_DEFINITIONS "${makecontext_CPPFLAGS} ${makecontext_CPPFLAGS_2}"
491                 )
492         file(READ ${simgrid_BINARY_DIR}/conftestval MAKECONTEXT_ADDR_SIZE)
493         string(REPLACE "\n" "" MAKECONTEXT_ADDR_SIZE "${MAKECONTEXT_ADDR_SIZE}")
494         string(REGEX MATCH ;^.*,;MAKECONTEXT_ADDR "${MAKECONTEXT_ADDR_SIZE}")
495         string(REGEX MATCH ;,.*$; MAKECONTEXT_SIZE "${MAKECONTEXT_ADDR_SIZE}")
496         string(REPLACE "," "" makecontext_addr "${MAKECONTEXT_ADDR}")
497         string(REPLACE "," "" makecontext_size "${MAKECONTEXT_SIZE}")   
498         set(pth_skaddr_makecontext "#define pth_skaddr_makecontext(skaddr,sksize) (${makecontext_addr})")
499         set(pth_sksize_makecontext "#define pth_sksize_makecontext(skaddr,sksize) (${makecontext_size})")
500
501 endif(HAVE_MAKECONTEXT OR WIN32)
502
503 #--------------------------------------------------------------------------------------------------
504
505 ### check for stackgrowth
506
507         try_run(RUN_makecontext_VAR COMPILE_makecontext_VAR
508                 ${PROJECT_DIRECTORY}
509                 ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_stackgrowth.c
510                 )
511 file(READ "${simgrid_BINARY_DIR}/conftestval" stack)
512 if(stack MATCHES "down")
513         set(PTH_STACKGROWTH "-1")
514 endif(stack MATCHES "down")
515 if(stack MATCHES "up")
516         set(PTH_STACKGROWTH "1")
517 endif(stack MATCHES "up")
518
519 ###############
520 ## System checks
521 ##
522
523 #SG_CONFIGURE_PART([System checks...])
524 #AC_PROG_CC(xlC gcc cc) -auto
525 #AM_SANITY_CHECK -auto
526
527 #AC_PROG_MAKE_SET
528
529
530 #AC_PRINTF_NULL
531 try_run(RUN_PRINTF_NULL_VAR COMPILE_PRINTF_NULL_VAR
532         ${PROJECT_DIRECTORY}
533         ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_printf_null.c
534         )
535
536 if(RUN_PRINTF_NULL_VAR MATCHES "FAILED_TO_RUN")
537 SET(PRINTF_NULL_WORKING "0")
538 else(RUN_PRINTF_NULL_VAR MATCHES "FAILED_TO_RUN")
539 SET(PRINTF_NULL_WORKING "1")
540 endif(RUN_PRINTF_NULL_VAR MATCHES "FAILED_TO_RUN")
541
542 #AC_CHECK_VA_COPY
543
544 set(diff_va "va_copy((d),(s))"
545 "VA_COPY((d),(s))"
546 "__va_copy((d),(s))"
547 "__builtin_va_copy((d),(s))"
548 "do { (d) = (s)\; } while (0)"
549 "do { *(d) = *(s)\; } while (0)"
550 "memcpy((void *)&(d), (void *)&(s)), sizeof((s))"
551 "memcpy((void *)(d), (void *)(s)), sizeof(*(s))"
552 )
553
554 foreach(fct ${diff_va})
555         write_file("${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_va_copy.c" "#include <stdlib.h>
556         #include <stdarg.h>
557         #include <string.h>
558         #define DO_VA_COPY(d,s) ${fct}
559         void test(char *str, ...)
560         {
561             va_list ap, ap2;
562             int i;
563             va_start(ap, str);
564             DO_VA_COPY(ap2, ap);
565             for (i = 1; i <= 9; i++) {
566                 int k = (int)va_arg(ap, int);
567                 if (k != i)
568                     abort();
569             }
570             DO_VA_COPY(ap, ap2);
571             for (i = 1; i <= 9; i++) {
572                 int k = (int)va_arg(ap, int);
573                 if (k != i)
574                     abort();
575             }
576             va_end(ap);
577         }
578         int main(int argc, char *argv[])
579         {
580             test("test", 1, 2, 3, 4, 5, 6, 7, 8, 9);
581             exit(0);
582         }"
583         )
584         try_compile(COMPILE_VA_NULL_VAR
585         ${PROJECT_DIRECTORY}
586         ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_va_copy.c
587         )
588         if(COMPILE_VA_NULL_VAR)
589                 string(REGEX REPLACE "\;" "" fctbis ${fct})
590                 if(${fctbis} STREQUAL "va_copy((d),(s))")
591                         set(HAVE_VA_COPY 1)
592                         set(ac_cv_va_copy "C99")
593                         set(__VA_COPY_USE_C99 "va_copy((d),(s))")       
594                 endif(${fctbis} STREQUAL "va_copy((d),(s))")
595
596                 if(${fctbis} STREQUAL "VA_COPY((d),(s))")
597                         set(ac_cv_va_copy "GCM")
598                         set(__VA_COPY_USE_GCM "VA_COPY((d),(s))")
599                 endif(${fctbis} STREQUAL "VA_COPY((d),(s))")
600
601                 if(${fctbis} STREQUAL "__va_copy((d),(s))")
602                         set(ac_cv_va_copy "GCH")
603                         set(__VA_COPY_USE_GCH "__va_copy((d),(s))")
604                 endif(${fctbis} STREQUAL "__va_copy((d),(s))")
605
606                 if(${fctbis} STREQUAL "__builtin_va_copy((d),(s))")
607                         set(ac_cv_va_copy "GCB")
608                         set(__VA_COPY_USE_GCB "__builtin_va_copy((d),(s))")
609                 endif(${fctbis} STREQUAL "__builtin_va_copy((d),(s))")
610
611                 if(${fctbis} STREQUAL "do { (d) = (s) } while (0)")
612                         set(ac_cv_va_copy "ASS")
613                         set(__VA_COPY_USE_ASS "do { (d) = (s); } while (0)")
614                 endif(${fctbis} STREQUAL "do { (d) = (s) } while (0)")
615
616                 if(${fctbis} STREQUAL "do { *(d) = *(s) } while (0)")
617                         set(ac_cv_va_copy "ASP")
618                         set(__VA_COPY_USE_ASP "do { *(d) = *(s); } while (0)")
619                 endif(${fctbis} STREQUAL "do { *(d) = *(s) } while (0)")
620
621                 if(${fctbis} STREQUAL "memcpy((void *)&(d), (void *)&(s)), sizeof((s))")
622                         set(ac_cv_va_copy "CPS")
623                         set(__VA_COPY_USE_CPS "memcpy((void *)&(d), (void *)&(s)), sizeof((s))")
624                 endif(${fctbis} STREQUAL "memcpy((void *)&(d), (void *)&(s)), sizeof((s))")
625
626                 if(${fctbis} STREQUAL "memcpy((void *)(d), (void *)(s)), sizeof(*(s))")
627                         set(ac_cv_va_copy "CPP")
628                         set(__VA_COPY_USE_CPP "memcpy((void *)(d), (void *)(s)), sizeof(*(s))")
629                 endif(${fctbis} STREQUAL "memcpy((void *)(d), (void *)(s)), sizeof(*(s))")
630                                 
631                 if(NOT STATUS_OK)
632                 set(__VA_COPY_USE "__VA_COPY_USE_${ac_cv_va_copy}(d, s)")
633                 endif(NOT STATUS_OK)
634                 set(STATUS_OK "1")
635                 
636         endif(COMPILE_VA_NULL_VAR)
637         
638 endforeach(fct ${diff_va})
639
640 #--------------------------------------------------------------------------------------------------
641 ### Try execut getline command
642 try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR
643         ${PROJECT_DIRECTORY}
644         ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_getline.c
645         OUTPUT_VARIABLE GETLINE_OUTPUT
646         )
647
648 if(NOT COMPILE_RESULT_VAR)
649 SET(need_getline "#define SIMGRID_NEED_GETLINE 1")
650 else(NOT COMPILE_RESULT_VAR)
651 SET(need_getline "")
652 endif(NOT COMPILE_RESULT_VAR)
653
654 ### check for a working snprintf
655 if(HAVE_SNPRINTF AND HAVE_VSNPRINTF)
656
657         try_run(RUN_SNPRINTF_FUNC_VAR COMPILE_SNPRINTF_FUNC_VAR
658                 ${PROJECT_DIRECTORY}
659                 ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_snprintf.c
660                 )
661         if(CMAKE_CROSSCOMPILING)
662                 set(RUN_SNPRINTF_FUNC "cross") 
663         endif(CMAKE_CROSSCOMPILING)
664
665         try_run(RUN_VSNPRINTF_FUNC_VAR COMPILE_VSNPRINTF_FUNC_VAR
666                 ${PROJECT_DIRECTORY}
667                 ${PROJECT_DIRECTORY}/buildtools/Cmake/test_prog/prog_vsnprintf.c
668                 )
669         if(CMAKE_CROSSCOMPILING)
670                 set(RUN_VSNPRINTF_FUNC "cross")
671         endif(CMAKE_CROSSCOMPILING)
672         set(PREFER_PORTABLE_SNPRINTF 0)
673         if(RUN_VSNPRINTF_FUNC_VAR MATCHES "FAILED_TO_RUN")
674                 set(PREFER_PORTABLE_SNPRINTF 1)
675         endif(RUN_VSNPRINTF_FUNC_VAR MATCHES "FAILED_TO_RUN")
676         if(RUN_SNPRINTF_FUNC_VAR MATCHES "FAILED_TO_RUN")
677                 set(PREFER_PORTABLE_SNPRINTF 1)
678         endif(RUN_SNPRINTF_FUNC_VAR MATCHES "FAILED_TO_RUN")
679 endif(HAVE_SNPRINTF AND HAVE_VSNPRINTF)
680
681 ### check for asprintf function familly
682 if(HAVE_ASPRINTF)
683         SET(need_asprintf "")
684 else(HAVE_ASPRINTF)
685         SET(need_asprintf "#define SIMGRID_NEED_ASPRINTF 1")
686 endif(HAVE_ASPRINTF)
687
688 if(HAVE_VASPRINTF)
689         SET(need_vasprintf "")
690 else(HAVE_VASPRINTF)
691         SET(need_vasprintf "#define SIMGRID_NEED_VASPRINTF 1")
692 endif(HAVE_VASPRINTF)
693
694 ### check for addr2line
695
696 find_path(ADDR2LINE NAMES addr2line     PATHS NO_DEFAULT_PATHS  )
697 if(ADDR2LINE)
698 set(ADDR2LINE "${ADDR2LINE}/addr2line")
699 endif(ADDR2LINE)
700
701 ### File to create
702
703 configure_file("${PROJECT_DIRECTORY}/src/context_sysv_config.h.in"                      "${PROJECT_DIRECTORY}/src/context_sysv_config.h" @ONLY IMMEDIATE)
704
705 SET( CMAKEDEFINE "#cmakedefine" )
706 configure_file("${PROJECT_DIRECTORY}/buildtools/Cmake/gras_config.h.in"         "${PROJECT_DIRECTORY}/src/gras_config.h" @ONLY IMMEDIATE)
707 configure_file("${PROJECT_DIRECTORY}/src/gras_config.h"                                 "${PROJECT_DIRECTORY}/src/gras_config.h" @ONLY IMMEDIATE)
708 configure_file("${PROJECT_DIRECTORY}/include/simgrid_config.h.in"                       "${PROJECT_DIRECTORY}/include/simgrid_config.h" @ONLY IMMEDIATE)
709 configure_file("${PROJECT_DIRECTORY}/buildtools/Cmake/tracing_config.h.in"      "${PROJECT_DIRECTORY}/include/instr/tracing_config.h" @ONLY IMMEDIATE)
710 configure_file("${PROJECT_DIRECTORY}/include/instr/tracing_config.h"            "${PROJECT_DIRECTORY}/include/instr/tracing_config.h" @ONLY IMMEDIATE)
711
712 set(top_srcdir "${PROJECT_DIRECTORY}")
713 set(srcdir "${PROJECT_DIRECTORY}/src")
714
715 set(exec_prefix ${prefix})
716 set(includedir ${prefix}/include)
717 set(top_builddir ${PROJECT_DIRECTORY})
718 set(libdir ${exec_prefix}/lib)
719 set(CMAKE_LINKARGS "${CMAKE_CURRENT_BINARY_DIR}/lib")
720
721 configure_file(${PROJECT_DIRECTORY}/src/smpi/smpicc.in ${CMAKE_CURRENT_BINARY_DIR}/bin/smpicc @ONLY)
722 configure_file(${PROJECT_DIRECTORY}/src/smpi/smpirun.in ${CMAKE_CURRENT_BINARY_DIR}/bin/smpirun @ONLY)
723
724 exec_program("chmod a=rwx ${CMAKE_CURRENT_BINARY_DIR}/bin/smpicc" OUTPUT_VARIABLE OKITOKI)
725 exec_program("chmod a=rwx ${CMAKE_CURRENT_BINARY_DIR}/bin/smpirun" OUTPUT_VARIABLE OKITOKI)