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