rts: Make LOOKS_LIKE_INFO_PTR a bit more strict
[ghc.git] / boot
1 #!/usr/bin/env python3
2
3 import glob
4 import os
5 import os.path
6 import sys
7 import argparse
8 from textwrap import dedent
9 import subprocess
10 import re
11
12 cwd = os.getcwd()
13
14 parser = argparse.ArgumentParser()
15 parser.add_argument('--validate', action='store_true', help='Run in validate mode')
16 parser.add_argument('--hadrian', action='store_true', help='Do not assume the make base build system')
17 args = parser.parse_args()
18
19 def print_err(s):
20     print(dedent(s), file=sys.stderr)
21
22 def die(mesg):
23     print_err(mesg)
24     sys.exit(1)
25
26 def check_for_url_rewrites():
27     if os.path.isdir('.git') and \
28        subprocess.check_output('git config remote.origin.url'.split()).find(b'github.com') != -1 and \
29        subprocess.call(['git', 'config', '--get-regexp', '^url.*github.com/.*/packages-.insteadOf']) != 0:
30         # If we cloned from github, make sure the url rewrites are set.
31         # Otherwise 'git submodule update --init' prints confusing errors.
32         die("""\
33             It seems you cloned this repository from GitHub. But your git config files
34             don't contain the url rewrites that are needed to make this work (GitHub
35             doesn't support '/' in repository names, so we use a different naming scheme
36             for the submodule repositories there).
37
38             Please run the following commands first:
39
40               git config --global url."git://github.com/ghc/packages-".insteadOf     git://github.com/ghc/packages/
41               git config --global url."http://github.com/ghc/packages-".insteadOf    http://github.com/ghc/packages/
42               git config --global url."https://github.com/ghc/packages-".insteadOf   https://github.com/ghc/packages/
43               git config --global url."ssh://git\@github.com/ghc/packages-".insteadOf ssh://git\@github.com/ghc/packages/
44               git config --global url."git\@github.com:/ghc/packages-".insteadOf      git\@github.com:/ghc/packages/
45
46             And then:
47
48               git submodule update --init
49               ./boot
50
51             Or start over, and clone the GHC repository from the haskell server:
52
53               git clone --recursive git://git.haskell.org/ghc.git
54
55             For more information, see:
56               * https://ghc.haskell.org/trac/ghc/wiki/Newcomers or
57               * https://ghc.haskell.org/trac/ghc/wiki/Building/GettingTheSources#CloningfromGitHub
58         """)
59
60 def check_boot_packages():
61     # Check that we have all boot packages.
62     import re
63     for l in open('packages', 'r'):
64         if l.startswith('#'):
65             continue
66
67         parts = [part for part in l.split(' ') if part]
68         if len(parts) != 4:
69             die("Error: Bad line in packages file: " + l)
70
71         dir_ = parts[0]
72         tag = parts[1]
73
74         # If tag is not "-" then it is an optional repository, so its
75         # absence isn't an error.
76         if tag == '-':
77             # We would like to just check for a .git directory here,
78             # but in an lndir tree we avoid making .git directories,
79             # so it doesn't exist. We therefore require that every repo
80             # has a LICENSE file instead.
81             license_path = os.path.join(dir_, 'LICENSE')
82             if not os.path.isfile(license_path):
83                 die("""\
84                     Error: %s doesn't exist
85                     Maybe you haven't run 'git submodule update --init'?
86                     """ % license_path)
87
88 # Create libraries/*/{ghc.mk,GNUmakefile}
89 def boot_pkgs():
90     library_dirs = []
91
92     for package in glob.glob("libraries/*/"):
93         packages_file = os.path.join(package, 'ghc-packages')
94         if os.path.isfile(packages_file):
95             for subpkg in open(packages_file, 'r'):
96                 library_dirs.append(os.path.join(package, subpkg.strip()))
97         else:
98             library_dirs.append(package)
99
100     for package in library_dirs:
101         if package[-1] == '/':
102             # drop trailing '/'
103             package = package[:-1]
104
105         dir_ = os.path.relpath(package, 'libraries')
106         cabals = glob.glob(os.path.join(package, '*.cabal.in'))
107         if len(cabals) == 0:
108             cabals = glob.glob(os.path.join(package, '*.cabal'))
109
110         if len(cabals) > 1:
111             die('Too many .cabal files in %s' % package)
112         elif len(cabals) == 1:
113             cabal = cabals[0]
114
115             if os.path.isfile(cabal):
116                 # strip both .cabal and .in
117                 pkg = os.path.splitext(os.path.splitext(os.path.basename(cabal))[0])[0]
118                 top = os.path.join(*['..'] * len(os.path.normpath(package).split(os.path.sep)))
119
120                 ghc_mk = os.path.join(package, 'ghc.mk')
121                 print('Creating %s' % ghc_mk)
122                 with open(ghc_mk, 'w') as f:
123                     f.write(dedent(
124                         """\
125                         {package}_PACKAGE = {pkg}
126                         {package}_dist-install_GROUP = libraries
127                         $(if $(filter {dir},$(PACKAGES_STAGE0)),$(eval $(call build-package,{package},dist-boot,0)))
128                         $(if $(filter {dir},$(PACKAGES_STAGE1)),$(eval $(call build-package,{package},dist-install,1)))
129                         $(if $(filter {dir},$(PACKAGES_STAGE2)),$(eval $(call build-package,{package},dist-install,2)))
130                         """.format(package = package,
131                                 pkg = pkg,
132                                 dir = dir_)))
133
134                 makefile = os.path.join(package, 'GNUmakefile')
135                 with open(makefile, 'w') as f:
136                     f.write(dedent(
137                         """\
138                         dir = {package}
139                         TOP = {top}
140                         include $(TOP)/mk/sub-makefile.mk
141                         FAST_MAKE_OPTS += stage=0
142                         """.format(package = package, top = top)
143                     ))
144
145
146 def autoreconf():
147     # Run autoreconf on everything that needs it.
148     processes = {}
149     if os.name == 'nt':
150         # Get the normalized ACLOCAL_PATH for Windows
151         # This is necessary since on Windows this will be a Windows
152         # path, which autoreconf doesn't know doesn't know how to handle.
153         ac_local = os.getenv('ACLOCAL_PATH', '')
154         ac_local_arg = re.sub(r';', r':', ac_local)
155         ac_local_arg = re.sub(r'\\', r'/', ac_local_arg)
156         ac_local_arg = re.sub(r'(\w):/', r'/\1/', ac_local_arg)
157         reconf_cmd = 'ACLOCAL_PATH=%s autoreconf' % ac_local_arg
158     else:
159         reconf_cmd = 'autoreconf'
160
161     for dir_ in ['.'] + glob.glob('libraries/*/'):
162         if os.path.isfile(os.path.join(dir_, 'configure.ac')):
163             print("Booting %s" % dir_)
164             processes[dir_] = subprocess.Popen(['sh', '-c', reconf_cmd], cwd=dir_)
165
166     # Wait for all child processes to finish.
167     fail = False
168     for k,v in processes.items():
169         code = v.wait()
170         if code != 0:
171             print_err('autoreconf in %s failed with exit code %d' % (k, code))
172             fail = True
173
174     if fail:
175         sys.exit(1)
176
177 def check_build_mk():
178     if not args.validate and not os.path.isfile("mk/build.mk"):
179         print(dedent(
180             """
181             WARNING: You don't have a mk/build.mk file.
182
183             By default a standard GHC build will be done, which uses optimisation
184             and builds the profiling libraries. This will take a long time, so may
185             not be what you want if you are developing GHC or the libraries, rather
186             than simply building it to use it.
187
188             For information on creating a mk/build.mk file, please see:
189                 http://ghc.haskell.org/trac/ghc/wiki/Building/Using#Buildconfiguration
190             """))
191
192 check_for_url_rewrites()
193 check_boot_packages()
194 if not args.hadrian:
195     boot_pkgs()
196 autoreconf()
197 if not args.hadrian:
198     check_build_mk()