.travis.install.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. # Part of `travis-lazarus` (https://github.com/nielsAD/travis-lazarus)
  3. # License: MIT
  4. import sys
  5. import os
  6. import subprocess
  7. OS_NAME=os.environ.get('TRAVIS_OS_NAME') or 'linux'
  8. OS_PMAN={'linux': 'sudo apt-get', 'osx': 'brew'}[OS_NAME]
  9. LAZ_TMP_DIR=os.environ.get('LAZ_TMP_DIR') or 'lazarus_tmp'
  10. LAZ_REL_DEF=os.environ.get('LAZ_REL_DEF') or {'linux':'amd64', 'qemu-arm':'amd64', 'qemu-arm-static':'amd64', 'osx':'i386', 'wine':'32'}
  11. LAZ_BIN_SRC=os.environ.get('LAZ_BIN_SRC') or 'http://mirrors.iwi.me/lazarus/releases/%(target)s/Lazarus%%20%(version)s'
  12. LAZ_BIN_TGT=os.environ.get('LAZ_BIN_TGT') or {
  13. 'linux': 'Lazarus%%20Linux%%20%(release)s%%20DEB',
  14. 'qemu-arm': 'Lazarus%%20Linux%%20%(release)s%%20DEB',
  15. 'qemu-arm-static': 'Lazarus%%20Linux%%20%(release)s%%20DEB',
  16. 'osx': 'Lazarus%%20Mac%%20OS%%20X%%20%(release)s',
  17. 'wine': 'Lazarus%%20Windows%%20%(release)s%%20bits'
  18. }
  19. def install_osx_dmg(dmg):
  20. try:
  21. # Mount .dmg file and parse (automatically determined) target volumes
  22. res = subprocess.check_output('sudo hdiutil attach %s | grep /Volumes/' % (dmg), shell=True)
  23. vol = ('/Volumes/' + l.strip().split('/Volumes/')[-1] for l in res.splitlines() if '/Volumes/' in l)
  24. except:
  25. return False
  26. # Install .pkg files with installer
  27. install_pkg = lambda v, f: os.system('sudo installer -pkg %s/%s -target /' % (v, f)) == 0
  28. for v in vol:
  29. try:
  30. if not all(map(lambda f: (not f.endswith('.pkg')) or install_pkg(v, f), os.listdir(v))):
  31. return False
  32. finally:
  33. # Unmount after installation
  34. os.system('hdiutil detach %s' % (v))
  35. return True
  36. def install_lazarus_default():
  37. if OS_NAME == 'linux':
  38. # Make sure nogui is installed for headless runs
  39. pkg = 'lazarus lcl-nogui'
  40. elif OS_NAME == 'osx':
  41. # Install brew cask first
  42. pkg = 'fpc caskroom/cask/brew-cask && %s cask install fpcsrc lazarus' % (OS_PMAN)
  43. else:
  44. # Default to lazarus
  45. pkg = 'lazarus'
  46. return os.system('%s install %s' % (OS_PMAN, pkg)) == 0
  47. def install_lazarus_version(ver,rel,env):
  48. # Download all files in directory for specified Lazarus version
  49. osn = env or OS_NAME
  50. tgt = LAZ_BIN_TGT[osn] % {'release': rel or LAZ_REL_DEF[osn]}
  51. src = LAZ_BIN_SRC % {'target': tgt, 'version': ver}
  52. if os.system('wget -r -l1 -T 30 -np -nd -nc -A .deb,.dmg,.exe %s -P %s' % (src, LAZ_TMP_DIR)) != 0:
  53. return False
  54. if osn == 'wine':
  55. # Install wine and Xvfb
  56. if os.system('sudo dpkg --add-architecture i386 && %s update && %s install xvfb wine' % (OS_PMAN, OS_PMAN)) != 0:
  57. return False
  58. # Initialize virtual display and wine directory
  59. if os.system('Xvfb %s & sleep 3 && wineboot -i' % (os.environ.get('DISPLAY') or '')) != 0:
  60. return False
  61. # Install basic Wine prerequisites, ignore failure
  62. os.system('winetricks -q corefonts')
  63. # Install all .exe files with wine
  64. process_file = lambda f: (not f.endswith('.exe')) or os.system('wine %s /VERYSILENT /DIR="c:\\lazarus"' % (f)) == 0
  65. elif osn == 'qemu-arm' or osn == 'qemu-arm-static':
  66. # Install qemu and arm cross compiling utilities
  67. if os.system('%s install libgtk2.0-dev qemu-user qemu-user-static binutils-arm-linux-gnueabi gcc-arm-linux-gnueabi' % (OS_PMAN)) != 0:
  68. return False
  69. # Install all .deb files (for linux) and cross compile later
  70. process_file = lambda f: (not f.endswith('.deb')) or os.system('sudo dpkg --force-overwrite -i %s' % (f)) == 0
  71. elif osn == 'linux':
  72. # Install dependencies
  73. if os.system('%s install libgtk2.0-dev' % (OS_PMAN)) != 0:
  74. return False
  75. # Install all .deb files
  76. process_file = lambda f: (not f.endswith('.deb')) or os.system('sudo dpkg --force-overwrite -i %s' % (f)) == 0
  77. elif osn == 'osx':
  78. # Install all .dmg files
  79. process_file = lambda f: (not f.endswith('.dmg')) or install_osx_dmg(f)
  80. else:
  81. return False
  82. # Process all downloaded files
  83. if not all(map(lambda f: process_file(os.path.join(LAZ_TMP_DIR, f)), sorted(os.listdir(LAZ_TMP_DIR)))):
  84. return False
  85. if osn == 'wine':
  86. # Set wine Path (persistently) to include Lazarus binary directory
  87. if os.system('wine cmd /C reg add HKEY_CURRENT_USER\\\\Environment /v PATH /t REG_SZ /d "%PATH%\\;c:\\\\lazarus"') != 0:
  88. return False
  89. # Redirect listed executables so they execute in wine
  90. for alias in ('fpc', 'lazbuild', 'lazarus'):
  91. os.system('echo "#!/usr/bin/env bash \nwine %(target)s \$@" | sudo tee %(name)s > /dev/null && sudo chmod +x %(name)s' % {
  92. 'target': subprocess.check_output("find $WINEPREFIX -iname '%s.exe' | head -1 " % (alias), shell=True).strip(),
  93. 'name': '/usr/bin/%s' % (alias)
  94. })
  95. elif osn == 'qemu-arm' or osn == 'qemu-arm-static':
  96. fpcv = subprocess.check_output('fpc -iV', shell=True).strip()
  97. gccv = subprocess.check_output('arm-linux-gnueabi-gcc -dumpversion', shell=True).strip()
  98. opts = ' '.join([
  99. 'CPU_TARGET=arm',
  100. 'OS_TARGET=linux',
  101. 'BINUTILSPREFIX=arm-linux-gnueabi-',
  102. # 'CROSSOPT="-CpARMV7A -CfVFPV3_D16"',
  103. 'OPT=-dFPC_ARMEL',
  104. 'INSTALL_PREFIX=/usr'
  105. ])
  106. # Compile ARM cross compiler
  107. if os.system('cd /usr/share/fpcsrc/%s && sudo make clean crossall crossinstall %s' % (fpcv, opts)) != 0:
  108. return False
  109. # Symbolic link to update default FPC cross compiler for ARM
  110. if os.system('sudo ln -sf /usr/lib/fpc/%s/ppcrossarm /usr/bin/ppcarm' % (fpcv)) != 0:
  111. return False
  112. # Update config file with paths to ARM libraries
  113. config = '\n'.join([
  114. '#INCLUDE /etc/fpc.cfg',
  115. '#IFDEF CPUARM',
  116. '-Xd','-Xt',
  117. '-XParm-linux-gnueabi-',
  118. '-Fl/usr/arm-linux-gnueabi/lib',
  119. '-Fl/usr/lib/gcc/arm-linux-gnueabi/%s' % (gccv),
  120. '-Fl/usr/lib/gcc-cross/arm-linux-gnueabi/%s' % (gccv),
  121. # '-CpARMV7A', '-CfVFPV3_D16',
  122. '#ENDIF',
  123. ''
  124. ])
  125. with open(os.path.expanduser('~/.fpc.cfg'),'w') as f:
  126. f.write(config)
  127. return True
  128. def install_lazarus(ver=None,rel=None,env=None):
  129. return install_lazarus_version(ver,rel,env) if ver else install_lazarus_default()
  130. def main():
  131. os.system('%s update' % (OS_PMAN))
  132. return install_lazarus(os.environ.get('LAZ_VER'),os.environ.get('LAZ_REL'),os.environ.get('LAZ_ENV'))
  133. if __name__ == '__main__':
  134. sys.exit(int(not main()))