1 import errno, glob, os, math, mimetypes, multiprocessing
2 import platform, readline, tarfile
3 from distutils import spawn
5 ## ------------------------
6 ## -- Create directories --
7 ## ------------------------
9 def create_dir( path ):
12 except OSError as exception:
13 if exception.errno != errno.EEXIST:
18 ## ------------------------
19 ## -- Filename completer --
20 ## ------------------------
22 def filename_completer( text, state ):
23 return ( glob.glob( text + '*' ) + [ None ] )[ state ]
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 ) )
37 def find_paths( locs, progs ):
41 path = spawn.find_executable( loc + "/" + prog )
49 ## ------------------------------
50 ## -- Extract compressed files --
51 ## ------------------------------
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 )
62 #elif src_type == tar_bz2_type:
63 #elif src_type == zip_type:
69 ## -------------------
70 ## -- Choose config --
71 ## -------------------
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 ] )
78 print( "\t" + str( len( paths ) + 0 ) + ": Give another installation." )
79 print( "\t" + str( len( paths ) + 1 ) + ": Build from scratch." )
82 opt = int( raw_input( "\t---> " ) )
83 if opt < 0 or opt > len( paths ) + 1:
90 ## --------------------
91 ## -- Some variables --
92 ## --------------------
94 home_dir = os.environ[ "HOME" ]
95 process_count = str( multiprocessing.cpu_count( ) / 2 )
97 ## -----------------------------------------------
98 ## -- qmake and cmake locations and executables --
99 ## -----------------------------------------------
104 if platform.system( ) == "Linux":
109 home_dir + "/local/bin"
111 qmake_progs = [ "qmake", "qmake-qt4" ]
112 cmake_progs = [ "cmake" ]
114 qmake_paths = find_paths( locations, qmake_progs )
115 cmake_paths = find_paths( locations, cmake_progs )
117 qmake_opt = choose_config( qmake_paths, "Qt4" )
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: " )
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: " )
130 qmake_exec = qmake_paths[ qmake_opt ]
133 cmake_opt = choose_config( cmake_paths, "CMake" )
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: " )
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: " )
146 cmake_exec = cmake_paths[ cmake_opt ]
149 ## -----------------------
150 ## -- ITK and VTK files --
151 ## -----------------------
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: " )
157 itk_extra = raw_input( "Type an extra configurations for ITK:" )
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: " )
163 vtk_extra = raw_input( "Type an extra configurations for VTK:" )
165 ## ------------------------
166 ## -- More configuration --
167 ## ------------------------
169 install_prefix = get_filepath( "Choose your installation prefix: " )
170 build_type = raw_input( "Type your build type [(r)elease/(d)ebug]: " )
173 if qmake_src <> None:
174 qmake_full = raw_input( "Would you like a full Qt4 install [y/n]? " )
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"
188 if qmake_src <> None:
189 qmake_src_dir = extract( qmake_src )
190 qmake_bin_dir = qmake_src_dir + "-build"
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
202 create_dir( qmake_bin_dir )
204 # particular_options=-no-framework
205 particular_options = ""
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"
217 command += particular_options
218 command += " && make -j" + process_count
219 command += " && make -j install"
221 qmake_exec = find_paths( [ install_prefix ], [ "qmake" ] )[ 0 ]
224 ## -------------------
225 ## -- Compile CMake --
226 ## -------------------
228 if cmake_src <> None:
229 cmake_src_dir = extract( cmake_src )
230 cmake_bin_dir = cmake_src_dir + "-build"
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
242 create_dir( cmake_bin_dir )
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"
251 cmake_exec = find_paths( [ install_prefix ], [ "cmake" ] )[ 0 ]
258 itk_src_dir = extract( itk_src )
259 itk_bin_dir = itk_src_dir + "-build"
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 )
271 create_dir( itk_bin_dir )
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"
294 vtk_src_dir = extract( vtk_src )
295 vtk_bin_dir = vtk_src_dir + "-build"
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 )
307 create_dir( vtk_bin_dir )
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"