2017-12-27 11:39:24 +01:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
#===============================================================================
|
|
|
|
|
# File: "macbuild/build4mac_util.py"
|
|
|
|
|
#
|
|
|
|
|
# Here are utility functions and classes ...
|
|
|
|
|
# for building KLayout (http://www.klayout.de/index.php)
|
|
|
|
|
# version 0.25 or later on different Apple Mac OSX platforms.
|
|
|
|
|
#
|
|
|
|
|
# This file is imported by 'build4mac.py' script.
|
|
|
|
|
#===============================================================================
|
|
|
|
|
from __future__ import print_function # to use print() of Python 3 in Python >= 2.7
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
2017-12-31 13:22:32 +01:00
|
|
|
import re
|
|
|
|
|
import string
|
2017-12-27 11:39:24 +01:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
## To import global dictionaries of different modules
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
mydir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
sys.path.append( mydir )
|
|
|
|
|
from build4mac_env import *
|
|
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
## To decompose strings obtained by 'otool -L <*.dylib>' command and to
|
2017-12-29 13:00:03 +01:00
|
|
|
# generate a dictionary of KLayout's inter-library dependency.
|
2017-12-27 11:39:24 +01:00
|
|
|
#
|
|
|
|
|
# @param[in] depstr strings that tell dependency such as:
|
|
|
|
|
#
|
2017-12-28 15:02:01 +01:00
|
|
|
# libklayout_edt.0.dylib:
|
2017-12-27 11:39:24 +01:00
|
|
|
# libklayout_edt.0.dylib (compatibility version 0.25.0, current version 0.25.0)
|
|
|
|
|
# libklayout_tl.0.dylib (compatibility version 0.25.0, current version 0.25.0)
|
|
|
|
|
# libklayout_gsi.0.dylib (compatibility version 0.25.0, current version 0.25.0)
|
|
|
|
|
# libklayout_laybasic.0.dylib (compatibility version 0.25.0, current version 0.25.0)
|
|
|
|
|
# libklayout_db.0.dylib (compatibility version 0.25.0, current version 0.25.0)
|
2017-12-29 13:00:03 +01:00
|
|
|
# :
|
|
|
|
|
# :
|
2017-12-27 11:39:24 +01:00
|
|
|
#
|
|
|
|
|
# @return a dictionary
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
def DecomposeLibraryDependency( depstr ):
|
|
|
|
|
alllines = depstr.split('\n')
|
|
|
|
|
numlines = len(alllines)
|
|
|
|
|
dependent = alllines[0].split(':')[0].strip(' ').strip('\t')
|
|
|
|
|
supporters = []
|
|
|
|
|
for i in range(1, numlines):
|
|
|
|
|
supporter = alllines[i].split(' ')[0].strip(' ').strip('\t')
|
|
|
|
|
if not supporter == '':
|
|
|
|
|
supporters.append(supporter)
|
|
|
|
|
return { dependent: supporters }
|
|
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
## To print the contents of a library dependency dictionary
|
|
|
|
|
#
|
|
|
|
|
# @param[in] depdic dictionary
|
|
|
|
|
# @param[in] namedic dictionary name
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
def PrintLibraryDependencyDictionary( depdic, namedic ):
|
|
|
|
|
keys = depdic.keys()
|
|
|
|
|
print("")
|
|
|
|
|
print("##### Contents of <%s> #####:" % namedic )
|
|
|
|
|
for key in keys:
|
|
|
|
|
supporters = depdic[key]
|
|
|
|
|
print( " %s:" % key )
|
|
|
|
|
for item in supporters:
|
|
|
|
|
print( " %s" % item )
|
|
|
|
|
|
2017-12-28 15:02:01 +01:00
|
|
|
#------------------------------------------------------------------------------
|
2017-12-29 13:00:03 +01:00
|
|
|
## To set and change identification name of KLayout's dylib
|
2017-12-28 15:02:01 +01:00
|
|
|
#
|
|
|
|
|
# @param[in] libdic inter-library dependency dictionary
|
|
|
|
|
#
|
|
|
|
|
# @return 0 on success; non-zero on failure
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
def SetChangeIdentificationNameOfDyLib( libdic ):
|
|
|
|
|
cmdNameId = XcodeToolChain['nameID']
|
|
|
|
|
cmdNameChg = XcodeToolChain['nameCH']
|
|
|
|
|
dependentLibs = libdic.keys()
|
|
|
|
|
|
|
|
|
|
for lib in dependentLibs:
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
|
# [1] Set the identification name of each dependent library
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
|
nameOld = "%s" % lib
|
|
|
|
|
nameNew = "@executable_path/../Frameworks/%s" % lib
|
|
|
|
|
command = "%s %s %s" % ( cmdNameId, nameNew, nameOld )
|
|
|
|
|
if subprocess.call( command, shell=True ) != 0:
|
|
|
|
|
msg = "!!! Failed to set the new identification name to <%s> !!!"
|
|
|
|
|
print( msg % lib, file=sys.stderr )
|
2018-01-01 00:42:13 +01:00
|
|
|
return 1
|
2017-12-28 15:02:01 +01:00
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
# [2] Make the library aware of the new identifications of all supporters
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
supporters = libdic[lib]
|
|
|
|
|
for sup in supporters:
|
|
|
|
|
nameOld = "%s" % sup
|
|
|
|
|
nameNew = "@executable_path/../Frameworks/%s" % sup
|
|
|
|
|
command = "%s %s %s %s" % ( cmdNameChg, nameOld, nameNew, lib )
|
|
|
|
|
if subprocess.call( command, shell=True ) != 0:
|
|
|
|
|
msg = "!!! Failed to make the library aware of the new identification name <%s> of supporter <%s> !!!"
|
|
|
|
|
print( msg % (nameNew, sup), file=sys.stderr )
|
2018-01-01 00:42:13 +01:00
|
|
|
return 1
|
2017-12-28 15:02:01 +01:00
|
|
|
# for-lib
|
2018-01-01 00:42:13 +01:00
|
|
|
return 0
|
2017-12-28 15:02:01 +01:00
|
|
|
|
2017-12-27 11:39:24 +01:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
## To set the identification names of KLayout's libraries to an executable
|
|
|
|
|
# and make the application aware of the library locations
|
|
|
|
|
#
|
|
|
|
|
# @param[in] executable path/to/executable -- (1)
|
|
|
|
|
# @param[in] relativedir directory of dylib relative to executable -- (2)
|
|
|
|
|
#
|
|
|
|
|
# Example: (1) "klayout.app/Contents/MacOS/klayout"
|
|
|
|
|
# (2) "../Frameworks"
|
|
|
|
|
#
|
|
|
|
|
# klayout.app/+
|
|
|
|
|
# +-- Contents/+
|
|
|
|
|
# +-- Info.plist
|
|
|
|
|
# +-- PkgInfo
|
|
|
|
|
# +-- Resources/
|
|
|
|
|
# +-- Frameworks/
|
2017-12-28 15:02:01 +01:00
|
|
|
# +-- MacOS/+
|
|
|
|
|
# | +-- 'klayout'
|
|
|
|
|
# +-- Buddy/+
|
|
|
|
|
# +-- 'strm2cif'
|
|
|
|
|
# +-- 'strm2dxf'
|
|
|
|
|
# :
|
|
|
|
|
# +-- 'strmxor'
|
2017-12-27 11:39:24 +01:00
|
|
|
#
|
|
|
|
|
# @return 0 on success; non-zero on failure
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
def SetChangeLibIdentificationName( executable, relativedir ):
|
|
|
|
|
cmdNameId = XcodeToolChain['nameID']
|
|
|
|
|
cmdNameChg = XcodeToolChain['nameCH']
|
|
|
|
|
otoolCm = "otool -L %s | grep libklayout" % executable
|
|
|
|
|
otoolOut = os.popen( otoolCm ).read()
|
|
|
|
|
exedepdic = DecomposeLibraryDependency( executable + ":\n" + otoolOut )
|
|
|
|
|
keys = exedepdic.keys()
|
|
|
|
|
deplibs = exedepdic[ keys[0] ]
|
2017-12-28 15:02:01 +01:00
|
|
|
|
2017-12-27 11:39:24 +01:00
|
|
|
for lib in deplibs:
|
|
|
|
|
#-----------------------------------------------------------
|
2017-12-28 15:02:01 +01:00
|
|
|
# [1] Set the identification names for the library
|
2017-12-27 11:39:24 +01:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
|
nameOld = "klayout.app/Contents/Frameworks/%s" % lib
|
|
|
|
|
nameNew = "@executable_path/%s/%s" % ( relativedir, lib )
|
|
|
|
|
command = "%s %s %s" % ( cmdNameId, nameNew, nameOld )
|
|
|
|
|
if subprocess.call( command, shell=True ) != 0:
|
2017-12-28 15:02:01 +01:00
|
|
|
msg = "!!! Failed to set the new identification name to <%s> !!!"
|
|
|
|
|
print( msg % lib, file=sys.stderr )
|
2018-01-01 00:42:13 +01:00
|
|
|
return 1
|
2017-12-28 15:02:01 +01:00
|
|
|
|
2017-12-27 11:39:24 +01:00
|
|
|
#-----------------------------------------------------------
|
2017-12-28 15:02:01 +01:00
|
|
|
# [2] Make the application aware of the new identification
|
2017-12-27 11:39:24 +01:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
|
nameOld = "%s" % lib
|
|
|
|
|
nameNew = "@executable_path/%s/%s" % ( relativedir, lib )
|
|
|
|
|
command = "%s %s %s %s" % ( cmdNameChg, nameOld, nameNew, executable )
|
|
|
|
|
if subprocess.call( command, shell=True ) != 0:
|
2017-12-28 15:02:01 +01:00
|
|
|
msg = "!!! Failed to make the application aware of the new identification name <%s> !!!"
|
|
|
|
|
print( msg % nameNew, file=sys.stderr )
|
2018-01-01 00:42:13 +01:00
|
|
|
return 1
|
2017-12-28 15:02:01 +01:00
|
|
|
# for-lib
|
2018-01-01 00:42:13 +01:00
|
|
|
return 0
|
2017-12-27 11:39:24 +01:00
|
|
|
|
2017-12-31 13:22:32 +01:00
|
|
|
#------------------------------------------------------------------------------
|
2018-01-01 00:42:13 +01:00
|
|
|
## To get KLayout's version from a file; most likely from 'version.sh'
|
2017-12-31 13:22:32 +01:00
|
|
|
#
|
|
|
|
|
# @param[in] verfile version file from which version is retrieved
|
|
|
|
|
#
|
|
|
|
|
# @return version string
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
def GetKLayoutVersionFrom( verfile='version.h' ):
|
|
|
|
|
version = "?.?.?"
|
|
|
|
|
try:
|
|
|
|
|
fd = open( verfile, "r" )
|
|
|
|
|
contents = fd.readlines()
|
|
|
|
|
fd.close()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return version
|
|
|
|
|
|
|
|
|
|
verReg = re.compile( u'(KLAYOUT_VERSION=\")([0-9A-Z_a-z\.]+)(\")' )
|
|
|
|
|
for line in contents:
|
|
|
|
|
m = verReg.match(line)
|
|
|
|
|
if m:
|
|
|
|
|
# print(m.group(0)) # KLAYOUT_VERSION="0.25"
|
|
|
|
|
# print(m.group(1)) # KLAYOUT_VERSION="
|
|
|
|
|
# print(m.group(2)) # 0.25
|
|
|
|
|
# print(m.group(3)) # "
|
|
|
|
|
version = m.group(2)
|
|
|
|
|
return version
|
|
|
|
|
return version
|
|
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
## To generate the contents of "Info.plist" file from a template
|
|
|
|
|
#
|
|
|
|
|
# @param[in] keydic dictionary of four key words ['exe', 'icon', 'bname', 'ver']
|
2018-01-01 00:42:13 +01:00
|
|
|
# @param[in] templfile template file ("macbuild/Resources/Info.plist.template")
|
2017-12-31 13:22:32 +01:00
|
|
|
#
|
|
|
|
|
# @return generated strings
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
def GenerateInfoPlist( keydic, templfile ):
|
|
|
|
|
val_exe = keydic['exe']
|
|
|
|
|
val_icon = keydic['icon']
|
|
|
|
|
val_bname = keydic['bname']
|
|
|
|
|
val_ver = keydic['ver']
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
fd = open( templfile, "r" )
|
|
|
|
|
template = fd.read()
|
|
|
|
|
fd.close()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return "???"
|
|
|
|
|
|
|
|
|
|
t = string.Template(template)
|
|
|
|
|
s = t.substitute( EXECUTABLE = val_exe,
|
|
|
|
|
ICONFILE = val_icon,
|
|
|
|
|
BUNDLENAME = val_bname,
|
|
|
|
|
VERSION = val_ver)
|
|
|
|
|
return s
|
|
|
|
|
|
2017-12-27 11:39:24 +01:00
|
|
|
#----------------
|
|
|
|
|
# End of File
|
|
|
|
|
#----------------
|