Stop generating make files when using hadrian.
[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     print(library_dirs)
102     for package in library_dirs:
103         if package[-1] == '/':
104             # drop trailing '/'
105             package = package[:-1]
106
107         dir_ = os.path.relpath(package, 'libraries')
108         cabals = glob.glob(os.path.join(package, '*.cabal.in'))
109         if len(cabals) == 0:
110             cabals = glob.glob(os.path.join(package, '*.cabal'))
111
112         if len(cabals) > 1:
113             die('Too many .cabal files in %s' % package)
114         elif len(cabals) == 1:
115             cabal = cabals[0]
116
117             if os.path.isfile(cabal):
118                 # strip both .cabal and .in
119                 pkg = os.path.splitext(os.path.splitext(os.path.basename(cabal))[0])[0]
120                 top = package
121
122                 ghc_mk = os.path.join(package, 'ghc.mk')
123                 print('Creating %s' % ghc_mk)
124                 with open(ghc_mk, 'w') as f:
125                     f.write(dedent(
126                         """\
127                         {package}_PACKAGE = {pkg}
128                         {package}_dist-install_GROUP = libraries
129                         $(if $(filter {dir},$(PACKAGES_STAGE0)),$(eval $(call build-package,{package},dist-boot,0)))
130                         $(if $(filter {dir},$(PACKAGES_STAGE1)),$(eval $(call build-package,{package},dist-install,1)))
131                         $(if $(filter {dir},$(PACKAGES_STAGE2)),$(eval $(call build-package,{package},dist-install,2)))
132                         """.format(package = package,
133                                 pkg = pkg,
134                                 dir = dir_)))
135
136
137 def autoreconf():
138     # Run autoreconf on everything that needs it.
139     processes = {}
140     if os.name == 'nt':
141         # Get the normalized ACLOCAL_PATH for Windows
142         # This is necessary since on Windows this will be a Windows
143         # path, which autoreconf doesn't know doesn't know how to handle.
144         ac_local = os.environ['ACLOCAL_PATH']
145         ac_local_arg = re.sub(r';', r':', ac_local)
146         ac_local_arg = re.sub(r'\\', r'/', ac_local_arg)
147         ac_local_arg = re.sub(r'(\w):/', r'/\1/', ac_local_arg)
148         reconf_cmd = 'ACLOCAL_PATH=%s autoreconf' % ac_local_arg
149     else:
150         reconf_cmd = 'autoreconf'
151
152     for dir_ in ['.'] + glob.glob('libraries/*/'):
153         if os.path.isfile(os.path.join(dir_, 'configure.ac')):
154             print("Booting %s" % dir_)
155             processes[dir_] = subprocess.Popen(['sh', '-c', reconf_cmd], cwd=dir_)
156
157     # Wait for all child processes to finish.
158     fail = False
159     for k,v in processes.items():
160         code = v.wait()
161         if code != 0:
162             print_err('autoreconf in %s failed with exit code %d' % (k, code))
163             fail = True
164
165     if fail:
166         sys.exit(1)
167
168 def check_build_mk():
169     if not args.validate and not os.path.isfile("mk/build.mk"):
170         print(dedent(
171             """
172             WARNING: You don't have a mk/build.mk file.
173
174             By default a standard GHC build will be done, which uses optimisation
175             and builds the profiling libraries. This will take a long time, so may
176             not be what you want if you are developing GHC or the libraries, rather
177             than simply building it to use it.
178
179             For information on creating a mk/build.mk file, please see:
180                 http://ghc.haskell.org/trac/ghc/wiki/Building/Using#Buildconfiguration
181             """))
182
183 check_for_url_rewrites()
184 if not args.hadrian:
185     boot_pkgs()
186 autoreconf()
187 if not args.hadrian:
188     check_build_mk()