]> Creatis software - cpPlugins.git/blob - dependencies/cpPlugins_Install_Dependencies.py
b1d7813c580f0e4440ca74f0619531ddf3df3f11
[cpPlugins.git] / dependencies / cpPlugins_Install_Dependencies.py
1 #!/usr/bin/python
2
3 import glob, multiprocessing, os, platform, readline
4 from distutils import spawn
5
6 ## --------------------
7 ## -- Various values --
8 ## --------------------
9
10 system_name = platform.system( )
11 process_count = multiprocessing.cpu_count( ) / 2
12 home_dir = os.environ[ "HOME" ]
13
14 ## ------------------------
15 ## -- Filename completer --
16 ## ------------------------
17
18 def filename_completer( text, state ):
19     return ( glob.glob( text + '*' ) + [ None ] )[ state ]
20 # fed
21
22 def get_filepath( message ):
23     readline.set_completer_delims( ' \t\n;' )
24     readline.parse_and_bind( "tab: complete" )
25     readline.set_completer( filename_completer )
26     return os.path.abspath( raw_input( message ) )
27 # fed
28
29 ## --------------------
30 ## -- File extractor --
31 ## --------------------
32
33 def file_extract( filename, outdir ):
34     real_ext = None
35     for ext in [ ".zip", ".tar", ".tar.gz", ".tar.bz2" ]:
36         if filename[ len( filename ) - len( ext ) : ] == ext:
37             real_ext = ext
38         # fi
39     # rof
40     command = None
41     if real_ext == ".zip":
42         command = "unzip " + filename + " -d " + outdir
43     elif real_ext == ".tar":
44         command = "tar xf " + filename + " -C " + outdir + " --strip-components=1"
45     elif real_ext == ".tar.gz":
46         command = "tar xzf " + filename + " -C " + outdir + " --strip-components=1"
47     elif real_ext == ".tar.bz2":
48         command = "tar xjf " + filename + " -C " + outdir + " --strip-components=1"
49     # fi
50     if command <> None:
51         print( "\tExtracting " + filename + "..." )
52         res = os.system( "mkdir -p " + outdir + " && " + command )
53         print( "\t\tdone." )
54         if res == 0:
55             return True
56         else:
57             return False
58     else:
59         return False
60     # fi
61 # fed
62
63 ## ------------------
64 ## -- Look for Qt4 --
65 ## ------------------
66
67 qmake_locations = []
68 qmake_progs = []
69 if system_name == "Linux":
70     qmake_locations = [
71         "/usr/bin",
72         "/usr/local/bin",
73         home_dir + "/bin",
74         home_dir + "/local/bin"
75         ]
76     qmake_progs = [ "qmake", "qmake-qt4" ]
77 # fi
78
79 qmake_paths = []
80 for prog in qmake_progs:
81     for loc in qmake_locations:
82         path = spawn.find_executable( loc + "/" + prog )
83         if path <> None:
84             qmake_paths.append( path )
85     # rof
86 # rof
87
88 print( "Please choose one configuration for Qt4:" )
89 for i in range( len( qmake_paths ) ):
90     print( "\t" + str( i ) + ": " + qmake_paths[ i ] )
91 # rof
92 print( "\t" + str( len( qmake_paths ) + 0 ) + ": Give another installation path (path to qmake)." )
93 print( "\t" + str( len( qmake_paths ) + 1 ) + ": Build from scratch." )
94 qmake_opt = -1
95 while qmake_opt == -1:
96     qmake_opt = int( raw_input( "\t---> " ) )
97     if qmake_opt < 0 or qmake_opt > len( qmake_paths ) + 1:
98         qmake_opt = -1
99     # fi
100 # elihw
101
102 qmake_exec = None
103 qmake_src = None
104 if qmake_opt == len( qmake_paths ):
105     qmake_exec = get_filepath( "Choose your own Qt4 installation: " )
106     while not os.path.isfile( qmake_exec ):
107         qmake_exec = get_filepath( "Choose your own Qt4 installation: " )
108     # elihw
109 elif qmake_opt == len( qmake_paths ) + 1:
110     qmake_src = get_filepath( "Choose your own Qt4 source file: " )
111     while not os.path.isfile( qmake_src ):
112         qmake_src = get_filepath( "Choose your own Qt4 source file: " )
113     # elihw
114 else:
115     qmake_exec = qmake_paths[ qmake_opt ]
116 # fi
117
118 ## --------------------
119 ## -- Look for CMake --
120 ## --------------------
121
122 cmake_locations = []
123 cmake_progs = []
124 if system_name == "Linux":
125     cmake_locations = [
126         "/usr/bin",
127         "/usr/local/bin",
128         home_dir + "/bin",
129         home_dir + "/local/bin"
130         ]
131     cmake_progs = [ "cmake" ]
132 # fi
133
134 cmake_paths = []
135 for prog in cmake_progs:
136     for loc in cmake_locations:
137         path = spawn.find_executable( loc + "/" + prog )
138         if path <> None:
139             cmake_paths.append( path )
140     # rof
141 # rof
142
143 print( "Please choose one configuration for CMake:" )
144 for i in range( len( cmake_paths ) ):
145     print( "\t" + str( i ) + ": " + cmake_paths[ i ] )
146 # rof
147 print( "\t" + str( len( cmake_paths ) + 0 ) + ": Give another installation path (path to cmake)." )
148 print( "\t" + str( len( cmake_paths ) + 1 ) + ": Build from scratch." )
149 cmake_opt = -1
150 while cmake_opt == -1:
151     cmake_opt = int( raw_input( "\t---> " ) )
152     if cmake_opt < 0 or cmake_opt > len( cmake_paths ) + 1:
153         cmake_opt = -1
154     # fi
155 # elihw
156
157 cmake_exec = None
158 cmake_src = None
159 if cmake_opt == len( cmake_paths ):
160     cmake_exec = get_filepath( "Choose your own CMake installation: " )
161     while not os.path.isfile( cmake_exec ):
162         cmake_exec = get_filepath( "Choose your own CMake installation: " )
163     # elihw
164 elif cmake_opt == len( cmake_paths ) + 1:
165     cmake_src = get_filepath( "Choose your own CMake source file: " )
166     while not os.path.isfile( cmake_src ):
167         cmake_src = get_filepath( "Choose your own CMake source file: " )
168     # elihw
169 else:
170     cmake_exec = cmake_paths[ cmake_opt ]
171 # fi
172
173 ## ------------------
174 ## -- Look for ITK --
175 ## ------------------
176
177 itk_src = get_filepath( "Choose your own ITK source file: " )
178 while not os.path.isfile( itk_src ):
179     itk_src = get_filepath( "Choose your own ITK source file: " )
180 # elihw
181
182 ## ------------------
183 ## -- Look for VTK --
184 ## ------------------
185
186 vtk_src = get_filepath( "Choose your own VTK source file: " )
187 while not os.path.isfile( vtk_src ):
188     vtk_src = get_filepath( "Choose your own VTK source file: " )
189 # elihw
190
191 ## --------------------------
192 ## -- Miscelaneous options --
193 ## --------------------------
194
195 build_prefix = get_filepath( "Build prefix: " )
196 install_prefix = get_filepath( "Installation prefix: " )
197 build_type = raw_input( "Build type [release/debug]: " ).lower( )
198
199 os.system( "mkdir -p " + build_prefix )
200 os.system( "mkdir -p " + install_prefix )
201
202 ## -------------------
203 ## -- Extract files --
204 ## -------------------
205
206 print( "Extracting source code... " )
207 if qmake_src <> None:
208     file_extract( qmake_src, build_prefix + "/qt4-source" )
209 # fi
210 if cmake_src <> None:
211     file_extract( cmake_src, build_prefix + "/cmake-source" )
212 # fi
213 file_extract( itk_src, build_prefix + "/itk-source" )
214 file_extract( vtk_src, build_prefix + "/vtk-source" )
215 print( " done!" )
216
217 ## ---------------
218 ## -- Build Qt4 --
219 ## ---------------
220
221 if qmake_src <> None:
222     full = raw_input( "\tWould you like to perform a full Qt4 build [y/n]? " )
223     full.lower( )
224     
225     qt4_source = os.path.abspath( build_prefix + "/qt4-source" )
226     qt4_build = os.path.abspath( build_prefix + "/qt4-build" )
227     os.system( "mkdir -p " + qt4_build )
228
229     config_command = qt4_source + "/configure"
230     config_command += " -prefix " + install_prefix
231     config_command += " -" + build_type
232     config_command += " -opensource -shared -fast -no-webkit -optimized-qmake -confirm-license"
233     if full[ 0 ] == "n":
234         config_command += " -no-phonon -no-phonon-backend -no-openvg -nomake demos -nomake examples"
235     # fi
236     ## MACOS: -no-framework
237
238     ## Build and install
239     res = os.system( "cd " + qt4_build + " && " + config_command )
240     if res <> 0:
241         print( "Error configuring Qt4." )
242         exit( 1 )
243     # fi
244     res = os.system( "cd " + qt4_build + " && make -j" + str( process_count ) )
245     if res <> 0:
246         print( "Error compiling Qt4." )
247         exit( 1 )
248     # fi
249     res = os.system( "cd " + qt4_build + " && make -j install" )
250     if res <> 0:
251         print( "Error installing Qt4." )
252         exit( 1 )
253     # fi
254     qmake_exec = install_prefix + "/bin/qmake"
255 # fi
256
257 ## -----------------
258 ## -- Build CMake --
259 ## -----------------
260
261 if cmake_src <> None:
262     cmake_source = os.path.abspath( build_prefix + "/cmake-source" )
263     cmake_build = os.path.abspath( build_prefix + "/cmake-build" )
264     os.system( "mkdir -p " + cmake_build )
265
266     config_command = cmake_source + "/bootstrap"
267     config_command += " --prefix " + install_prefix
268     if qmake_exec <> None:
269         config_command += " --qt-gui --qt-qmake=" + qmake_exec
270     else:
271         config_command += " --no-qt-gui"
272     # fi
273
274     ## Build and install
275     res = os.system( "cd " + cmake_build + " && " + config_command )
276     if res <> 0:
277         print( "Error configuring CMake." )
278         exit( 1 )
279     # fi
280     res = os.system( "cd " + cmake_build + " && make -j" + str( process_count ) )
281     if res <> 0:
282         print( "Error compiling CMake." )
283         exit( 1 )
284     # fi
285     res = os.system( "cd " + cmake_build + " && make -j install" )
286     if res <> 0:
287         print( "Error installing CMake." )
288         exit( 1 )
289     # fi
290     cmake_exec = install_prefix + "/bin/cmake"
291 # fi
292
293 ## ---------------
294 ## -- Build VTK --
295 ## ---------------
296
297 vtk_source = os.path.abspath( build_prefix + "/vtk-source" )
298 vtk_build = os.path.abspath( build_prefix + "/vtk-build" )
299 os.system( "mkdir -p " + vtk_build )
300
301 kitware_build = "MinSizeRel"
302 if build_type == "debug":
303     kitware_build = "Debug"
304 # fi
305
306 vtk_configure = cmake_exec
307 vtk_configure += " -DCMAKE_CXX_FLAGS:STRING=-std=c++11"
308 vtk_configure += " -DBUILD_DOCUMENTATION:BOOL=OFF"
309 vtk_configure += " -DBUILD_EXAMPLES:BOOL=OFF"
310 vtk_configure += " -DBUILD_SHARED_LIBS:BOOL=ON"
311 vtk_configure += " -DBUILD_TESTING:BOOL=OFF"
312 vtk_configure += " -DCMAKE_BUILD_TYPE:STRING=" + kitware_build
313 if qmake_exec <> None:
314     vtk_configure += " -DQT_QMAKE_EXECUTABLE:PATH=" + qmake_exec
315     vtk_configure += " -DModule_vtkGUISupportQt:BOOL=ON"
316     vtk_configure += " -DModule_vtkGUISupportQtOpenGL:BOOL=ON"
317     vtk_configure += " -DModule_vtkGUISupportQtSQL:BOOL=OFF"
318     vtk_configure += " -DModule_vtkGUISupportQtWebkit:BOOL=OFF"
319 # fi
320 vtk_configure += " -DCMAKE_INSTALL_PREFIX:PATH=" + install_prefix
321 vtk_configure += " " + vtk_source
322     
323 ## Build and install
324 res = os.system( "cd " + vtk_build + " && " + vtk_configure )
325 if res <> 0:
326     print( "Error configuring VTK." )
327     exit( 1 )
328 # fi
329 res = os.system( "cd " + vtk_build + " && make -j" + str( process_count ) )
330 if res <> 0:
331     print( "Error compiling VTK." )
332     exit( 1 )
333 # fi
334 res = os.system( "cd " + vtk_build + " && make -j install" )
335 if res <> 0:
336     print( "Error installing VTK." )
337     exit( 1 )
338 # fi
339
340 ## ---------------
341 ## -- Build ITK --
342 ## ---------------
343
344 itk_source = os.path.abspath( build_prefix + "/itk-source" )
345 itk_build = os.path.abspath( build_prefix + "/itk-build" )
346 os.system( "mkdir -p " + itk_build )
347
348 kitware_build = "MinSizeRel"
349 if build_type == "debug":
350     kitware_build = "Debug"
351 # fi
352
353 itk_configure = cmake_exec
354 itk_configure += " -DCMAKE_CXX_FLAGS:STRING=-std=c++11"
355 itk_configure += " -DBUILD_DOCUMENTATION:BOOL=OFF"
356 itk_configure += " -DBUILD_EXAMPLES:BOOL=OFF"
357 itk_configure += " -DBUILD_SHARED_LIBS:BOOL=ON"
358 itk_configure += " -DBUILD_TESTING:BOOL=OFF"
359 itk_configure += " -DCMAKE_BUILD_TYPE:STRING=" + kitware_build
360 itk_configure += " -DModule_ITKReview:BOOL=ON"
361 itk_configure += " -DModule_ITKVtkGlue:BOOL=OFF"
362 itk_configure += " -DModule_ParabolicMorphology:BOOL=ON"
363 itk_configure += " -DCMAKE_INSTALL_PREFIX:PATH=" + install_prefix
364 itk_configure += " " + itk_source
365
366 ## Build and install
367 res = os.system( "cd " + itk_build + " && " + itk_configure )
368 if res <> 0:
369     print( "Error configuring ITK." )
370     exit( 1 )
371 # fi
372 res = os.system( "cd " + itk_build + " && make -j" + str( process_count ) )
373 if res <> 0:
374     print( "Error compiling ITK." )
375     exit( 1 )
376 # fi
377 res = os.system( "cd " + itk_build + " && make -j install" )
378 if res <> 0:
379     print( "Error installing ITK." )
380     exit( 1 )
381 # fi
382
383 ## eof - $RCSfile$