Line endings fixes in bakefile-generated files.
[wxWidgets.git] / build / tools / build-wxwidgets.py
1 #!/usr/bin/env python
2
3 ###################################
4 # Author: Kevin Ollivier
5 # Licence: wxWindows licence
6 ###################################
7
8 import os
9 import re
10 import sys
11 import builder
12 import glob
13 import optparse
14 import platform
15 import shutil
16 import types
17 import subprocess
18
19 # builder object
20 wxBuilder = None
21
22 # other globals
23 scriptDir = None
24 wxRootDir = None
25 contribDir = None
26 options = None
27 configure_opts = None
28 exitWithException = True
29 nmakeCommand = 'nmake.exe'
30
31 verbose = False
32
33
34 def numCPUs():
35 """
36 Detects the number of CPUs on a system.
37 This approach is from detectCPUs here: http://www.artima.com/weblogs/viewpost.jsp?thread=230001
38 """
39 # Linux, Unix and MacOS:
40 if hasattr(os, "sysconf"):
41 if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
42 # Linux & Unix:
43 ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
44 if isinstance(ncpus, int) and ncpus > 0:
45 return ncpus
46 else: # OSX:
47 p = subprocess.Popen("sysctl -n hw.ncpu", shell=True, stdout=subprocess.PIPE)
48 return p.stdout.read()
49
50 # Windows:
51 if "NUMBER_OF_PROCESSORS" in os.environ:
52 ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
53 if ncpus > 0:
54 return ncpus
55 return 1 # Default
56
57
58 def getXcodePath():
59 return getoutput("xcode-select -print-path")
60
61
62 def getVisCVersion():
63 text = getoutput("cl.exe")
64 if 'Version 13' in text:
65 return '71'
66 if 'Version 15' in text:
67 return '90'
68 if 'Version 16' in text:
69 return '100'
70 # TODO: Add more tests to get the other versions...
71 else:
72 return 'FIXME'
73
74
75 def exitIfError(code, msg):
76 if code != 0:
77 print(msg)
78 if exitWithException:
79 raise builder.BuildError(msg)
80 else:
81 sys.exit(1)
82
83
84 def getWxRelease(wxRoot=None):
85 if not wxRoot:
86 global wxRootDir
87 wxRoot = wxRootDir
88
89 configureText = open(os.path.join(wxRoot, "configure.in"), "r").read()
90 majorVersion = re.search("wx_major_version_number=(\d+)", configureText).group(1)
91 minorVersion = re.search("wx_minor_version_number=(\d+)", configureText).group(1)
92
93 versionText = "%s.%s" % (majorVersion, minorVersion)
94
95 if int(minorVersion) % 2:
96 releaseVersion = re.search("wx_release_number=(\d+)", configureText).group(1)
97 versionText += ".%s" % (releaseVersion)
98
99 return versionText
100
101
102 def getFrameworkName(options):
103 # the name of the framework is based on the wx port being built
104 name = "wxOSX"
105 if options.osx_cocoa:
106 name += "Cocoa"
107 else:
108 name += "Carbon"
109 return name
110
111
112 def getPrefixInFramework(options, wxRoot=None):
113 # the path inside the framework that is the wx --prefix
114 fwPrefix = os.path.join(
115 os.path.abspath(options.mac_framework_prefix),
116 "%s.framework/Versions/%s" % (getFrameworkName(options), getWxRelease(wxRoot)))
117 return fwPrefix
118
119
120 def macFixupInstallNames(destdir, prefix, buildDir=None):
121 # When an installdir is used then the install_names embedded in
122 # the dylibs are not correct. Reset the IDs and the dependencies
123 # to use just the prefix.
124 print("**** macFixupInstallNames(%s, %s, %s)" % (destdir, prefix, buildDir))
125 pwd = os.getcwd()
126 os.chdir(destdir+prefix+'/lib')
127 dylibs = glob.glob('*.dylib') # ('*[0-9].[0-9].[0-9].[0-9]*.dylib')
128 for lib in dylibs:
129 cmd = 'install_name_tool -id %s/lib/%s %s/lib/%s' % \
130 (prefix,lib, destdir+prefix,lib)
131 print(cmd)
132 run(cmd)
133 for dep in dylibs:
134 if buildDir is not None:
135 cmd = 'install_name_tool -change %s/lib/%s %s/lib/%s %s/lib/%s' % \
136 (buildDir,dep, prefix,dep, destdir+prefix,lib)
137 else:
138 cmd = 'install_name_tool -change %s/lib/%s %s/lib/%s %s/lib/%s' % \
139 (destdir+prefix,dep, prefix,dep, destdir+prefix,lib)
140 print(cmd)
141 run(cmd)
142 os.chdir(pwd)
143
144
145 def run(cmd):
146 global verbose
147 if verbose:
148 print("Running %s" % cmd)
149 return exitIfError(os.system(cmd), "Error running %s" % cmd)
150
151
152 def getoutput(cmd):
153 sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
154 output = None
155 output = sp.stdout.read()
156 if sys.version_info > (3,):
157 output = output.decode('utf-8') # TODO: is utf-8 okay here?
158 output = output.rstrip()
159 rval = sp.wait()
160 if rval:
161 # Failed!
162 print("Command '%s' failed with exit code %d." % (cmd, rval))
163 sys.exit(rval)
164 return output
165
166
167 def main(scriptName, args):
168 global scriptDir
169 global wxRootDir
170 global contribDir
171 global options
172 global configure_opts
173 global wxBuilder
174 global nmakeCommand
175
176 scriptDir = os.path.dirname(os.path.abspath(scriptName))
177 wxRootDir = os.path.abspath(os.path.join(scriptDir, "..", ".."))
178
179 contribDir = os.path.join("contrib", "src")
180 installDir = None
181
182 VERSION = tuple([int(i) for i in getWxRelease().split('.')[:2]])
183
184 if sys.platform.startswith("win"):
185 contribDir = os.path.join(wxRootDir, "contrib", "build")
186
187 if sys.platform.startswith("win"):
188 toolkit = "msvc"
189 else:
190 toolkit = "autoconf"
191
192 defJobs = str(numCPUs())
193 defFwPrefix = '/Library/Frameworks'
194
195 option_dict = {
196 "clean" : (False, "Clean all files from the build directory"),
197 "debug" : (False, "Build the library in debug symbols"),
198 "builddir" : ("", "Directory where the build will be performed for autoconf builds."),
199 "prefix" : ("", "Configured prefix to use for autoconf builds. Defaults to installdir if set. Ignored for framework builds."),
200 "jobs" : (defJobs, "Number of jobs to run at one time in make. Default: %s" % defJobs),
201 "install" : (False, "Install the toolkit to the installdir directory, or the default dir."),
202 "installdir" : ("", "Directory where built wxWidgets will be installed"),
203 "mac_distdir" : (None, "If set on Mac, will create an installer package in the specified dir."),
204 "mac_universal_binary"
205 : ("", "Comma separated list of architectures to include in the Mac universal binary"),
206 "mac_framework" : (False, "Install the Mac build as a framework"),
207 "mac_framework_prefix"
208 : (defFwPrefix, "Prefix where the framework should be installed. Default: %s" % defFwPrefix),
209 "cairo" : (False, "Enable dynamicly loading the Cairo lib for wxGraphicsContext on MSW"),
210 "no_config" : (False, "Turn off configure step on autoconf builds"),
211 "config_only" : (False, "Only run the configure step and then exit"),
212 "rebake" : (False, "Regenerate Bakefile and autoconf files"),
213 "unicode" : (False, "Build the library with unicode support"),
214 "wxpython" : (False, "Build the wxWidgets library with all options needed by wxPython"),
215 "cocoa" : (False, "Build the old Mac Cooca port."),
216 "osx_cocoa" : (False, "Build the new Cocoa port"),
217 "shared" : (False, "Build wx as a dynamic library"),
218 "extra_make" : ("", "Extra args to pass on [n]make's command line."),
219 "features" : ("", "A comma-separated list of wxUSE_XYZ defines on Win, or a list of configure flags on unix."),
220 "verbose" : (False, "Print commands as they are run, (to aid with debugging this script)"),
221 "jom" : (False, "Use jom.exe instead of nmake for MSW builds."),
222 }
223
224 parser = optparse.OptionParser(usage="usage: %prog [options]", version="%prog 1.0")
225
226 keys = option_dict.keys()
227 for opt in sorted(keys):
228 default = option_dict[opt][0]
229 action = "store"
230 if type(default) == bool:
231 action = "store_true"
232 parser.add_option("--" + opt, default=default, action=action, dest=opt,
233 help=option_dict[opt][1])
234
235 options, arguments = parser.parse_args(args=args)
236
237 global verbose
238 if options.verbose:
239 verbose = True
240
241 # compiler / build system specific args
242 buildDir = options.builddir
243 args = []
244 installDir = options.installdir
245 prefixDir = options.prefix
246
247 if toolkit == "autoconf":
248 if not buildDir:
249 buildDir = os.getcwd()
250 configure_opts = []
251 if options.features != "":
252 configure_opts.extend(options.features.split(" "))
253
254 if options.unicode:
255 configure_opts.append("--enable-unicode")
256
257 if options.debug:
258 configure_opts.append("--enable-debug")
259
260 if options.cocoa:
261 configure_opts.append("--with-old_cocoa")
262
263 if options.osx_cocoa:
264 configure_opts.append("--with-osx_cocoa")
265
266 wxpy_configure_opts = [
267 "--with-opengl",
268 "--enable-sound",
269 "--enable-graphics_ctx",
270 "--enable-mediactrl",
271 "--enable-display",
272 "--enable-geometry",
273 "--enable-debug_flag",
274 "--enable-optimise",
275 "--disable-debugreport",
276 "--enable-uiactionsim",
277 ]
278
279 if sys.platform.startswith("darwin"):
280 wxpy_configure_opts.append("--enable-monolithic")
281 else:
282 wxpy_configure_opts.append("--with-sdl")
283 wxpy_configure_opts.append("--with-gnomeprint")
284
285 # Try to use use lowest available SDK back to 10.5. Both Carbon and
286 # Cocoa builds require at least the 10.5 SDK now. We only add it to
287 # the wxpy options because this is a hard-requirement for wxPython,
288 # but other cases it is optional and is left up to the developer.
289 # TODO: there should be a command line option to set the SDK...
290 if sys.platform.startswith("darwin"):
291 xcodePath = getXcodePath()
292 sdks = [
293 xcodePath+"/SDKs/MacOSX10.5.sdk",
294 xcodePath+"/SDKs/MacOSX10.6.sdk",
295 xcodePath+"/SDKs/MacOSX10.7.sdk",
296 ]
297
298 # use the lowest available sdk
299 for sdk in sdks:
300 if os.path.exists(sdk):
301 wxpy_configure_opts.append(
302 "--with-macosx-sdk=%s" % sdk)
303 break
304
305 if not options.mac_framework:
306 if installDir and not prefixDir:
307 prefixDir = installDir
308 if prefixDir:
309 prefixDir = os.path.abspath(prefixDir)
310 configure_opts.append("--prefix=" + prefixDir)
311
312
313 if options.wxpython:
314 configure_opts.extend(wxpy_configure_opts)
315 if options.debug:
316 # wxPython likes adding these debug options too
317 configure_opts.append("--enable-debug_gdb")
318 configure_opts.append("--disable-optimise")
319 configure_opts.remove("--enable-optimise")
320
321
322 if options.rebake:
323 retval = run("make -f autogen.mk")
324 exitIfError(retval, "Error running autogen.mk")
325
326 if options.mac_framework:
327 # TODO: Should options.install be automatically turned on if the
328 # mac_framework flag is given?
329
330 # framework builds always need to be monolithic
331 if not "--enable-monolithic" in configure_opts:
332 configure_opts.append("--enable-monolithic")
333
334 # The --prefix given to configure will be the framework prefix
335 # plus the framework specific dir structure.
336 prefixDir = getPrefixInFramework(options)
337 configure_opts.append("--prefix=" + prefixDir)
338
339 # the framework build adds symlinks above the installDir + prefixDir folder
340 # so we need to wipe from the framework root instead of inside the prefixDir.
341 frameworkRootDir = os.path.abspath(os.path.join(installDir + prefixDir, "..", ".."))
342 if os.path.exists(frameworkRootDir):
343 if os.path.exists(frameworkRootDir):
344 shutil.rmtree(frameworkRootDir)
345
346 if options.mac_universal_binary:
347 if options.mac_universal_binary == 'default':
348 if options.osx_cocoa:
349 configure_opts.append("--enable-universal_binary=i386,x86_64")
350 else:
351 configure_opts.append("--enable-universal_binary")
352 else:
353 configure_opts.append("--enable-universal_binary=%s" % options.mac_universal_binary)
354
355
356 print("Configure options: " + repr(configure_opts))
357 wxBuilder = builder.AutoconfBuilder()
358 if not options.no_config and not options.clean:
359 olddir = os.getcwd()
360 if buildDir:
361 os.chdir(buildDir)
362 exitIfError(wxBuilder.configure(dir=wxRootDir, options=configure_opts),
363 "Error running configure")
364 os.chdir(olddir)
365
366 if options.config_only:
367 print("Exiting after configure")
368 return
369
370 elif toolkit in ["msvc", "msvcProject"]:
371 flags = {}
372 buildDir = os.path.abspath(os.path.join(scriptDir, "..", "msw"))
373
374 print("creating wx/msw/setup.h from setup0.h")
375 if options.unicode:
376 flags["wxUSE_UNICODE"] = "1"
377 if VERSION < (2,9):
378 flags["wxUSE_UNICODE_MSLU"] = "1"
379
380 if options.cairo:
381 if not os.environ.get("CAIRO_ROOT"):
382 print("WARNING: Expected CAIRO_ROOT set in the environment!")
383 flags["wxUSE_CAIRO"] = "1"
384
385 if options.wxpython:
386 flags["wxDIALOG_UNIT_COMPATIBILITY "] = "0"
387 flags["wxUSE_DEBUGREPORT"] = "0"
388 flags["wxUSE_DIALUP_MANAGER"] = "0"
389 flags["wxUSE_GRAPHICS_CONTEXT"] = "1"
390 flags["wxUSE_DISPLAY"] = "1"
391 flags["wxUSE_GLCANVAS"] = "1"
392 flags["wxUSE_POSTSCRIPT"] = "1"
393 flags["wxUSE_AFM_FOR_POSTSCRIPT"] = "0"
394 flags["wxUSE_DATEPICKCTRL_GENERIC"] = "1"
395
396 if VERSION < (2,9):
397 flags["wxUSE_DIB_FOR_BITMAP"] = "1"
398
399 if VERSION >= (2,9):
400 flags["wxUSE_UIACTIONSIMULATOR"] = "1"
401
402
403 mswIncludeDir = os.path.join(wxRootDir, "include", "wx", "msw")
404 setup0File = os.path.join(mswIncludeDir, "setup0.h")
405 setupText = open(setup0File, "rb").read()
406
407 for flag in flags:
408 setupText, subsMade = re.subn(flag + "\s+?\d", "%s %s" % (flag, flags[flag]), setupText)
409 if subsMade == 0:
410 print("Flag %s wasn't found in setup0.h!" % flag)
411 sys.exit(1)
412
413 setupFile = open(os.path.join(mswIncludeDir, "setup.h"), "wb")
414 setupFile.write(setupText)
415 setupFile.close()
416 args = []
417 if toolkit == "msvc":
418 print("setting build options...")
419 args.append("-f makefile.vc")
420 if options.unicode:
421 args.append("UNICODE=1")
422 if VERSION < (2,9):
423 args.append("MSLU=1")
424
425 if options.wxpython:
426 args.append("OFFICIAL_BUILD=1")
427 args.append("COMPILER_VERSION=%s" % getVisCVersion())
428 args.append("SHARED=1")
429 args.append("MONOLITHIC=0")
430 args.append("USE_OPENGL=1")
431 args.append("USE_GDIPLUS=1")
432
433 if not options.debug:
434 args.append("BUILD=release")
435 else:
436 args.append("BUILD=debug")
437
438 if options.shared:
439 args.append("SHARED=1")
440
441 if options.cairo:
442 args.append(
443 "CPPFLAGS=/I%s" %
444 os.path.join(os.environ.get("CAIRO_ROOT", ""), 'include\\cairo'))
445
446 if options.jom:
447 nmakeCommand = 'jom.exe'
448
449 wxBuilder = builder.MSVCBuilder(commandName=nmakeCommand)
450
451 if toolkit == "msvcProject":
452 args = []
453 if options.shared or options.wxpython:
454 args.append("wx_dll.dsw")
455 else:
456 args.append("wx.dsw")
457
458 # TODO:
459 wxBuilder = builder.MSVCProjectBuilder()
460
461
462 if not wxBuilder:
463 print("Builder not available for your specified platform/compiler.")
464 sys.exit(1)
465
466 if options.clean:
467 print("Performing cleanup.")
468 wxBuilder.clean(dir=buildDir, options=args)
469
470 sys.exit(0)
471
472 if options.extra_make:
473 args.append(options.extra_make)
474
475 if not sys.platform.startswith("win"):
476 args.append("--jobs=" + options.jobs)
477 exitIfError(wxBuilder.build(dir=buildDir, options=args), "Error building")
478
479 if options.install:
480 extra=None
481 if installDir:
482 extra = ['DESTDIR='+installDir]
483 wxBuilder.install(dir=buildDir, options=extra)
484
485 if options.install and options.mac_framework:
486
487 def renameLibrary(libname, frameworkname):
488 reallib = libname
489 links = []
490 while os.path.islink(reallib):
491 links.append(reallib)
492 reallib = "lib/" + os.readlink(reallib)
493
494 #print("reallib is %s" % reallib)
495 run("mv -f %s lib/%s.dylib" % (reallib, frameworkname))
496
497 for link in links:
498 run("ln -s -f %s.dylib %s" % (frameworkname, link))
499
500 frameworkRootDir = prefixDir
501 if installDir:
502 print("installDir = %s" % installDir)
503 frameworkRootDir = installDir + prefixDir
504 os.chdir(frameworkRootDir)
505 build_string = ""
506 if options.debug:
507 build_string = "d"
508
509 fwname = getFrameworkName(options)
510 version = getoutput("bin/wx-config --release")
511 version_full = getoutput("bin/wx-config --version")
512 basename = getoutput("bin/wx-config --basename")
513 configname = getoutput("bin/wx-config --selected-config")
514
515 os.makedirs("Resources")
516 wxplist = dict(
517 CFBundleDevelopmentRegion="English",
518 CFBundleIdentifier='org.wxwidgets.wxosxcocoa',
519 CFBundleName=fwname,
520 CFBundleVersion=version_full,
521 CFBundleExecutable=fwname,
522 CFBundleGetInfoString="%s %s" % (fwname, version_full),
523 CFBundlePackageType="FMWK",
524 CFBundleSignature="WXCO",
525 CFBundleShortVersionString=version_full,
526 CFBundleInfoDictionaryVersion="6.0",
527 )
528
529 import plistlib
530 plistlib.writePlist(wxplist, os.path.join(frameworkRootDir, "Resources", "Info.plist"))
531
532 # we make wx the "actual" library file and link to it from libwhatever.dylib
533 # so that things can link to wx and survive minor version changes
534 renameLibrary("lib/lib%s-%s.dylib" % (basename, version), fwname)
535 run("ln -s -f lib/%s.dylib %s" % (fwname, fwname))
536
537 run("ln -s -f include Headers")
538
539 for lib in ["GL", "STC", "Gizmos", "Gizmos_xrc"]:
540 libfile = "lib/lib%s_%s-%s.dylib" % (basename, lib.lower(), version)
541 if os.path.exists(libfile):
542 frameworkDir = "framework/wx%s/%s" % (lib, version)
543 if not os.path.exists(frameworkDir):
544 os.makedirs(frameworkDir)
545 renameLibrary(libfile, "wx" + lib)
546 run("ln -s -f ../../../%s %s/wx%s" % (libfile, frameworkDir, lib))
547
548 for lib in glob.glob("lib/*.dylib"):
549 if not os.path.islink(lib):
550 corelibname = "lib/lib%s-%s.0.dylib" % (basename, version)
551 run("install_name_tool -id %s %s" % (os.path.join(prefixDir, lib), lib))
552 run("install_name_tool -change %s %s %s" % (os.path.join(frameworkRootDir, corelibname), os.path.join(prefixDir, corelibname), lib))
553
554 os.chdir("include")
555
556 header_template = """
557 #ifndef __WX_FRAMEWORK_HEADER__
558 #define __WX_FRAMEWORK_HEADER__
559
560 %s
561
562 #endif // __WX_FRAMEWORK_HEADER__
563 """
564 headers = ""
565 header_dir = "wx-%s/wx" % version
566 for include in glob.glob(header_dir + "/*.h"):
567 headers += "#include <wx/" + os.path.basename(include) + ">\n"
568
569 framework_header = open("%s.h" % fwname, "w")
570 framework_header.write(header_template % headers)
571 framework_header.close()
572
573 run("ln -s -f %s wx" % header_dir)
574 os.chdir("wx-%s/wx" % version)
575 run("ln -s -f ../../../lib/wx/include/%s/wx/setup.h setup.h" % configname)
576
577 os.chdir(os.path.join(frameworkRootDir, ".."))
578 run("ln -s -f %s Current" % getWxRelease())
579 os.chdir("..")
580 run("ln -s -f Versions/Current/Headers Headers")
581 run("ln -s -f Versions/Current/Resources Resources")
582 run("ln -s -f Versions/Current/%s %s" % (fwname, fwname))
583
584 # sanity check to ensure the symlink works
585 os.chdir("Versions/Current")
586
587 # put info about the framework into wx-config
588 os.chdir(frameworkRootDir)
589 text = file('lib/wx/config/%s' % configname).read()
590 text = text.replace("MAC_FRAMEWORK=", "MAC_FRAMEWORK=%s" % getFrameworkName(options))
591 if options.mac_framework_prefix not in ['/Library/Frameworks',
592 '/System/Library/Frameworks']:
593 text = text.replace("MAC_FRAMEWORK_PREFIX=",
594 "MAC_FRAMEWORK_PREFIX=%s" % options.mac_framework_prefix)
595 file('lib/wx/config/%s' % configname, 'w').write(text)
596
597 # The framework is finished!
598 print("wxWidgets framework created at: " +
599 os.path.join( installDir,
600 options.mac_framework_prefix,
601 '%s.framework' % fwname))
602
603
604 # adjust the install_name if needed
605 if sys.platform.startswith("darwin") and \
606 options.install and \
607 options.installdir and \
608 not options.mac_framework and \
609 not options.wxpython: # wxPython's build will do this later if needed
610 if not prefixDir:
611 prefixDir = '/usr/local'
612 macFixupInstallNames(options.installdir, prefixDir)#, buildDir)
613
614 # make a package if a destdir was set.
615 if options.mac_framework and \
616 options.install and \
617 options.installdir and \
618 options.mac_distdir:
619
620 if os.path.exists(options.mac_distdir):
621 shutil.rmtree(options.mac_distdir)
622
623 packagedir = os.path.join(options.mac_distdir, "packages")
624 os.makedirs(packagedir)
625 basename = os.path.basename(prefixDir.split(".")[0])
626 packageName = basename + "-" + getWxRelease()
627 packageMakerPath = getXcodePath()+"/usr/bin/packagemaker "
628 args = []
629 args.append("--root %s" % options.installdir)
630 args.append("--id org.wxwidgets.%s" % basename.lower())
631 args.append("--title %s" % packageName)
632 args.append("--version %s" % getWxRelease())
633 args.append("--out %s" % os.path.join(packagedir, packageName + ".pkg"))
634 cmd = packageMakerPath + ' '.join(args)
635 print("cmd = %s" % cmd)
636 run(cmd)
637
638 os.chdir(options.mac_distdir)
639
640 run('hdiutil create -srcfolder %s -volname "%s" -imagekey zlib-level=9 %s.dmg' % (packagedir, packageName, packageName))
641
642 shutil.rmtree(packagedir)
643
644 if __name__ == '__main__':
645 exitWithException = False # use sys.exit instead
646 main(sys.argv[0], sys.argv[1:])
647