]> Creatis software - cpPlugins.git/blob - dependencies/dependencies.py
...
[cpPlugins.git] / dependencies / dependencies.py
1 import errno, glob, os, math, mimetypes, multiprocessing
2 import platform, readline, tarfile
3 from distutils import spawn
4
5 ## ------------------------
6 ## -- Create directories --
7 ## ------------------------
8
9 def create_dir( path ):
10     try:
11         os.makedirs(path)
12     except OSError as exception:
13         if exception.errno != errno.EEXIST:
14             raise
15     # yrt
16 # fed
17         
18 ## ------------------------
19 ## -- Filename completer --
20 ## ------------------------
21
22 def filename_completer( text, state ):
23     return ( glob.glob( text + '*' ) + [ None ] )[ state ]
24 # fed
25
26 def get_filepath( message ):
27     readline.set_completer_delims( ' \t\n;' )
28     readline.parse_and_bind( "tab: complete" )
29     readline.set_completer( filename_completer )
30     return os.path.abspath( raw_input( message ) )
31 # fed
32
33 ## ----------------
34 ## -- Find paths --
35 ## ----------------
36
37 def find_paths( locs, progs ):
38     paths = []
39     for prog in progs:
40         for loc in locs:
41             path = spawn.find_executable( loc + "/" + prog )
42             if path <> None:
43                 paths.append( path )
44         # rof
45     # rof
46     return paths
47 # def
48
49 ## ------------------------------
50 ## -- Extract compressed files --
51 ## ------------------------------
52
53 def extract( src ):
54     tar_gz_type = ( "application/x-tar", "gzip" )
55     src_type = mimetypes.guess_type( src )
56     print( "Extracting " + src + ": " )
57     if src_type == tar_gz_type:
58         src_dir = os.path.splitext( src )[ 0 ]
59         src_file = tarfile.open( src, "r|gz" )
60         src_file.extractall( src_dir )
61         src_file.close( )
62     #elif src_type == tar_bz2_type:
63     #elif src_type == zip_type:
64     #else:
65     # fi
66     print( " done!" )
67     return src_dir
68
69 ## -------------------
70 ## -- Choose config --
71 ## -------------------
72
73 def choose_config( paths, name ):
74     print( "Please choose one configuration for " + name + ":" )
75     for i in range( len( paths ) ):
76         print( "\t" + str( i ) + ": " + paths[ i ] )
77     # rof
78     print( "\t" + str( len( paths ) + 0 ) + ": Give another installation." )
79     print( "\t" + str( len( paths ) + 1 ) + ": Build from scratch." )
80     opt = -1
81     while opt == -1:
82         opt = int( raw_input( "\t---> " ) )
83         if opt < 0 or opt > len( paths ) + 1:
84             qmake_opt = -1
85         # fi
86     # elihw
87     return opt
88 # def
89
90 ## --------------------
91 ## -- Some variables --
92 ## --------------------
93
94 home_dir = os.environ[ "HOME" ]
95 process_count = str( multiprocessing.cpu_count( ) / 2 )
96
97 ## -----------------------------------------------
98 ## -- qmake and cmake locations and executables --
99 ## -----------------------------------------------
100
101 locations = []
102 qmake_progs = []
103 cmake_progs = []
104 if platform.system( ) == "Linux":
105     locations = [
106         "/usr/bin",
107         "/usr/local/bin",
108         home_dir + "/bin",
109         home_dir + "/local/bin"
110         ]
111     qmake_progs = [ "qmake", "qmake-qt4" ]
112     cmake_progs = [ "cmake" ]
113 # fi
114 qmake_paths = find_paths( locations, qmake_progs )
115 cmake_paths = find_paths( locations, cmake_progs )
116
117 qmake_opt = choose_config( qmake_paths, "Qt4" )
118 qmake_src = None
119 if qmake_opt == len( qmake_paths ):
120     qmake_exec = get_filepath( "Choose your own Qt4 installation: " )
121     while not os.path.isfile( qmake_exec ):
122         qmake_exec = get_filepath( "Choose your own Qt4 installation: " )
123     # elihw
124 elif qmake_opt == len( qmake_paths ) + 1:
125     qmake_src = get_filepath( "Choose your own Qt4 source file: " )
126     while not os.path.isfile( qmake_src ):
127         qmake_src = get_filepath( "Choose your own Qt4 source file: " )
128     # elihw
129 else:
130     qmake_exec = qmake_paths[ qmake_opt ]
131 # fi
132
133 cmake_opt = choose_config( cmake_paths, "CMake" )
134 cmake_src = None
135 if cmake_opt == len( cmake_paths ):
136     cmake_exec = get_filepath( "Choose your own CMake installation: " )
137     while not os.path.isfile( cmake_exec ):
138         cmake_exec = get_filepath( "Choose your own CMake installation: " )
139     # elihw
140 elif cmake_opt == len( cmake_paths ) + 1:
141     cmake_src = get_filepath( "Choose your own CMake source file: " )
142     while not os.path.isfile( cmake_src ):
143         cmake_src = get_filepath( "Choose your own CMake source file: " )
144     # elihw
145 else:
146     cmake_exec = cmake_paths[ cmake_opt ]
147 # fi
148
149 ## -----------------------
150 ## -- ITK and VTK files --
151 ## -----------------------
152
153 itk_src = get_filepath( "Choose your own ITK source file: " )
154 while not os.path.isfile( itk_src ):
155     itk_src = get_filepath( "Choose your own ITK source file: " )
156 # elihw
157 itk_extra = raw_input( "Type an extra configurations for ITK:" )
158
159 vtk_src = get_filepath( "Choose your own VTK source file: " )
160 while not os.path.isfile( vtk_src ):
161     vtk_src = get_filepath( "Choose your own VTK source file: " )
162 # elihw
163 vtk_extra = raw_input( "Type an extra configurations for VTK:" )
164
165 ## ------------------------
166 ## -- More configuration --
167 ## ------------------------
168
169 install_prefix = get_filepath( "Choose your installation prefix: " )
170 build_type = raw_input( "Type your build type [(r)elease/(d)ebug]: " )
171 build_type.lower( )
172 qmake_full = "n"
173 if qmake_src <> None:
174     qmake_full = raw_input( "Would you like a full Qt4 install [y/n]? " )
175     qmake_full.lower( )
176 # fi
177 qmake_build_type = "release"
178 kitware_build_type = "MinSizeRel"
179 if build_type[ 0 ] == "d":
180     qmake_build_type = "debug"
181     kitware_build_type = "Debug"
182 # fi
183
184 ## -----------------
185 ## -- Compile Qt4 --
186 ## -----------------
187
188 if qmake_src <> None:
189     qmake_src_dir = extract( qmake_src )
190     qmake_bin_dir = qmake_src_dir + "-build"
191     min_size = 999999
192     qmake_configure = ""
193     for root, dirs, files in os.walk( qmake_src_dir ):
194         if "configure" in files:
195             path = os.path.join( root, "configure" )
196             if min_size > len( path ):
197                 min_size = len( path )
198                 qmake_configure = path
199             # fi
200         # fi
201     # rof
202     create_dir( qmake_bin_dir )
203
204     # particular_options=-no-framework
205     particular_options = ""
206
207     command  = "cd " + qmake_bin_dir + " && "
208     command += qmake_configure
209     command += " -prefix " + install_prefix
210     command += " -" + qmake_build_type
211     command += " -opensource -shared -fast -no-webkit -optimized-qmake"
212     command += " -confirm-license"
213     if qmake_full == "y":
214         command += " -no-phonon -no-phonon-backend -no-openvg"
215         command += " -nomake demos -nomake examples"
216     # fi
217     command += particular_options
218     command += " && make -j" + process_count
219     command += " && make -j install"
220     os.system( command )
221     qmake_exec = find_paths( [ install_prefix ], [ "qmake" ] )[ 0 ]
222 # fi
223
224 ## -------------------
225 ## -- Compile CMake --
226 ## -------------------
227
228 if cmake_src <> None:
229     cmake_src_dir = extract( cmake_src )
230     cmake_bin_dir = cmake_src_dir + "-build"
231     min_size = 999999
232     cmake_configure = ""
233     for root, dirs, files in os.walk( cmake_src_dir ):
234         if "bootstrap" in files:
235             path = os.path.join( root, "bootstrap" )
236             if min_size > len( path ):
237                 min_size = len( path )
238                 cmake_configure = path
239             # fi
240         # fi
241     # rof
242     create_dir( cmake_bin_dir )
243
244     command  = "cd " + cmake_bin_dir + " && "
245     command += cmake_configure
246     command += " --prefix " + install_prefix
247     command += " --qt-gui --qt-qmake=" + qmake_exec
248     command += " && make -j" + process_count
249     command += " && make -j install"
250     os.system( command )
251     cmake_exec = find_paths( [ install_prefix ], [ "cmake" ] )[ 0 ]
252 # fi
253
254 ## -----------------
255 ## -- Compile ITK --
256 ## -----------------
257
258 itk_src_dir = extract( itk_src )
259 itk_bin_dir = itk_src_dir + "-build"
260 min_size = 999999
261 itk_configure = ""
262 for root, dirs, files in os.walk( itk_src_dir ):
263     if "CMakeLists.txt" in files:
264         path = os.path.join( root, "CMakeLists.txt" )
265         if min_size > len( path ):
266             min_size = len( path )
267             itk_configure = path
268         # fi
269     # fi
270 # rof
271 create_dir( itk_bin_dir )
272
273 command  = "cd " + itk_bin_dir + " && "
274 command += cmake_exec
275 command += " -DCMAKE_CXX_FLAGS:STRING=-std=c++11"
276 command += " -DBUILD_DOCUMENTATION:BOOL=OFF"
277 command += " -DBUILD_EXAMPLES:BOOL=OFF"
278 command += " -DBUILD_SHARED_LIBS:BOOL=ON"
279 command += " -DBUILD_TESTING:BOOL=OFF"
280 command += " -DModule_ITKReview:BOOL=ON"
281 command += " -DModule_ITKVtkGlue:BOOL=OFF"
282 command += " -DModule_ParabolicMorphology:BOOL=ON"
283 command += " -DCMAKE_BUILD_TYPE:STRING=" + kitware_build_type
284 command += " -DCMAKE_INSTALL_PREFIX:PATH=" + install_prefix
285 command += " " + itk_src_dir
286 command += " && make -j" + process_count
287 command += " && make -j install"
288 os.system( command )
289
290 ## -----------------
291 ## -- Compile VTK --
292 ## -----------------
293
294 vtk_src_dir = extract( vtk_src )
295 vtk_bin_dir = vtk_src_dir + "-build"
296 min_size = 999999
297 vtk_configure = ""
298 for root, dirs, files in os.walk( vtk_src_dir ):
299     if "CMakeLists.txt" in files:
300         path = os.path.join( root, "CMakeLists.txt" )
301         if min_size > len( path ):
302             min_size = len( path )
303             vtk_configure = path
304         # fi
305     # fi
306 # rof
307 create_dir( vtk_bin_dir )
308
309 command  = "cd " + vtk_bin_dir + " && "
310 command += cmake_exec
311 command += " -DCMAKE_CXX_FLAGS:STRING=-std=c++11"
312 command += " -DBUILD_DOCUMENTATION:BOOL=OFF"
313 command += " -DBUILD_EXAMPLES:BOOL=OFF"
314 command += " -DBUILD_SHARED_LIBS:BOOL=ON"
315 command += " -DBUILD_TESTING:BOOL=OFF"
316 command += " -DModule_vtkGUISupportQt:BOOL=ON"
317 command += " -DModule_vtkGUISupportQtOpenGL:BOOL=ON"
318 command += " -DModule_vtkGUISupportQtSQL:BOOL=OFF"
319 command += " -DModule_vtkGUISupportQtWebkit:BOOL=OFF"
320 command += " -DQT_QMAKE_EXECUTABLE:PATH=" + qmake_exec
321 command += " -DCMAKE_BUILD_TYPE:STRING=" + kitware_build_type
322 command += " -DCMAKE_INSTALL_PREFIX:PATH=" + install_prefix
323 command += " " + vtk_src_dir
324 command += " && make -j" + process_count
325 command += " && make -j install"
326 os.system( command )
327
328 ## eof - $RCSfile$