1import shutil
2import glob
3import os
4import pdb
5from subprocess import call
6
7def change_link(link,new_link, bin_file):
8    arguments = ['-change',  link, new_link, bin_file]
9    retcode = call(['install_name_tool'] + arguments)
10    if retcode != 0:
11        print "Failed ---------"
12    print "changed link:", link, "to", new_link, 'in', bin_file, 'ret:', retcode
13
14def change_id(new_id, file_name):
15    arguments = ['-id',  new_id, file_name]
16    retcode = call(['install_name_tool'] + arguments)
17    print "changed id:", new_id, 'in', file_name, 'ret:', retcode
18
19def adjust_link(old_link, new_link, app_name, bin_name, suffix = '64'):
20
21    change_id(new_link, app_name + '/Contents/Frameworks/' + new_link[new_link.rindex('/') + 1:])
22    change_link(old_link, new_link, app_name + '/Contents/Frameworks/CsoundLib%s.framework/Versions/5.2/CsoundLib%s'%(suffix, suffix))
23    change_link(old_link, new_link, app_name + '/Contents/Frameworks/CsoundLib%s.framework/Versions/5.2/lib_csnd.dylib'%suffix)
24
25def copy_files(app_name, bin_name, doubles=True):
26    suffix = '64' if doubles else ''
27    cs_framework = '/Library/Frameworks/CsoundLib%s.framework'%suffix
28    try:
29        shutil.copytree(cs_framework, app_name + '/Contents/Frameworks/CsoundLib%s.framework'%suffix, symlinks=True )
30    except Exception:
31        print "Warning: Csound Framework not copied"
32        pass
33
34    libs = { 'libsndfile.1.dylib': 'libsndfile.1.dylib',
35             'libportaudio.2.dylib': 'libportaudio.dylib',
36             'libportmidi.dylib': 'libportmidi.dylib',
37             #'libmpadec.dylib': 'libmpadec.dylib',
38             'liblo.0.dylib' : 'liblo.dylib',
39             'libfltk.1.3.dylib' : 'libfltk.dylib',
40             'libfltk_images.1.3.dylib' : 'libfltk_images.dylib',
41             'libfluidsynth.1.dylib' : 'libfluidsynth.dylib',
42             'libpng12.0.dylib' : 'libpng12.dylib',
43             'libfluidsynth.1.dylib' : 'libfluidsynth.dylib'}
44
45    lib_dir = '/usr/local/lib/'
46    for lib, dest_lib in libs.items():
47        shutil.copy(lib_dir + lib, app_name + '/Contents/Frameworks/' + dest_lib )
48        adjust_link('/usr/local/lib/' + lib , '@executable_path/../Frameworks/' + dest_lib, app_name, bin_name, suffix)
49
50    change_link('/Library/Frameworks/CsoundLib%s.framework/Versions/5.2/CsoundLib%s'%(suffix,suffix),
51            '@executable_path/../Frameworks/CsoundLib%s.framework/Versions/5.2/CsoundLib%s'%(suffix,suffix),
52            bin_name)
53    change_link('/Library/Frameworks/CsoundLib%s.framework/Versions/5.2/lib_csnd.dylib'%suffix,
54            '@executable_path/../Frameworks/CsoundLib%s.framework/Versions/5.2/lib_csnd.dylib'%suffix,
55            bin_name)
56    change_link('/Library/Frameworks/CsoundLib%s.framework/Versions/5.2/CsoundLib%s'%(suffix,suffix),
57            '@executable_path/../Frameworks/CsoundLib%s.framework/Versions/5.2/CsoundLib%s'%(suffix,suffix),
58            app_name + '/Contents/Frameworks/CsoundLib%s.framework/Versions/5.2/lib_csnd.dylib'%suffix)
59
60
61    change_link('/usr/local/lib/libfltk.1.3.dylib',
62            '@executable_path/../libfltk.dylib',
63            app_name + '/Contents/Frameworks/libfltk_images.dylib')
64    change_link('/usr/local/lib/libpng12.0.dylib',
65            '@executable_path/../libpng12.dylib',
66            app_name + '/Contents/Frameworks/libfltk_images.dylib')
67
68    opcode_dir = '/Content/Frameworks/CsoundLib%s.framework/Resources/Opcodes%s'%(suffix,suffix)
69    opcode_libs = glob.glob(app_name + opcode_dir + '/*.dylib')
70
71    for op_lib in opcode_libs:
72        for dep_lib, dep_dest_lib in libs.items():
73            change_link('/usr/local/lib/' + dep_lib , '@executable_path/../Frameworks/' + dep_dest_lib,
74                    op_lib)
75            change_link('/usr/local/lib/' + dep_lib , '@executable_path/../Frameworks/' + dep_dest_lib,
76                    op_lib)
77            change_link('/Library/Frameworks/CsoundLib%s.framework/Versions/5.2/CsoundLib%s'%(suffix,suffix),
78                    '@executable_path/../Frameworks/CsoundLib%s.framework/Versions/5.2/CsoundLib%s'%(suffix,suffix),
79                    op_lib)
80
81if (not os.path.exists('build')):
82    os.mkdir("build")
83
84os.chdir("build")
85qute_app_dir = 'bin/'
86qtbindir = '/Users/acabrera/QtSDK/Desktop/Qt/4.8.1/gcc/bin/'
87qmake = qtbindir + "qmake"
88macdeployqt = qtbindir + "macdeployqt"
89
90doubles = False
91os.system(qmake + " /Users/acabrera/src/qutecsound/trunk/qutecsound/QuteApp/QuteApp.pro -r -spec macx-g++ CONFIG+=release CONFIG+=buildFloats")
92#os.system("make clean")
93os.system("make -w")
94binary = 'QuteApp_' + ('d' if doubles else 'f')
95pdb.set_trace()
96os.system(macdeployqt + ' ' + qute_app_dir + binary + '.app')
97copy_files(qute_app_dir + binary + '.app', qute_app_dir + binary + '.app/Contents/MacOS/' + binary, doubles)
98if (os.path.exists(qute_app_dir + '../../../src/res/osx/' + binary + '.app')):
99    shutil.rmtree(qute_app_dir + '../../../src/res/osx/' + binary + '.app')
100shutil.copytree(qute_app_dir + binary + '.app', qute_app_dir + '../../../src/res/osx/' + binary + '.app' , symlinks=True )
101
102doubles = True
103os.system(qmake + " /Users/acabrera/src/qutecsound/trunk/qutecsound/QuteApp/QuteApp.pro -r -spec macx-g++ CONFIG+=release CONFIG+=buildDoubles")
104#os.system("make clean")
105os.system("make -w")
106binary = 'QuteApp_' + ('d' if doubles else 'f')
107os.system(macdeployqt + ' ' + qute_app_dir + binary + '.app')
108copy_files(qute_app_dir + binary + '.app', qute_app_dir + binary + '.app/Contents/MacOS/' + binary, doubles)
109if (os.path.exists(qute_app_dir + '../../../src/res/osx/' + binary + '.app')):
110    shutil.rmtree(qute_app_dir + '../../../src/res/osx/' + binary + '.app')
111shutil.copytree(qute_app_dir + binary + '.app', qute_app_dir + '../../../src/res/osx/' + binary + '.app' , symlinks=True )
112
113
114