summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/build_tools/make_rules.py
blob: a24e8c8368378aac08a93b5d580229574aefb041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os


#
# Default macros for various platforms.
#
NEWLIB_DEFAULTS = """
NEWLIB_CC?=$(TC_PATH)/$(OSNAME)_x86_newlib/bin/i686-nacl-gcc -c
NEWLIB_CXX?=$(TC_PATH)/$(OSNAME)_x86_newlib/bin/i686-nacl-g++ -c -std=gnu++98
NEWLIB_LINK?=$(TC_PATH)/$(OSNAME)_x86_newlib/bin/i686-nacl-g++ -Wl,-as-needed
NEWLIB_LIB?=$(TC_PATH)/$(OSNAME)_x86_newlib/bin/i686-nacl-ar r
NEWLIB_DUMP?=$(TC_PATH)/$(OSNAME)_x86_newlib/x86_64-nacl/bin/objdump
NEWLIB_CCFLAGS?=-MMD -pthread $(NACL_WARNINGS) -idirafter $(NACL_SDK_ROOT)/include
NEWLIB_LDFLAGS?=-pthread
"""

GLIBC_DEFAULTS = """
GLIBC_CC?=$(TC_PATH)/$(OSNAME)_x86_glibc/bin/i686-nacl-gcc -c
GLIBC_CXX?=$(TC_PATH)/$(OSNAME)_x86_glibc/bin/i686-nacl-g++ -c -std=gnu++98
GLIBC_LINK?=$(TC_PATH)/$(OSNAME)_x86_glibc/bin/i686-nacl-g++ -Wl,-as-needed
GLIBC_LIB?=$(TC_PATH)/$(OSNAME)_x86_glibc/bin/i686-nacl-ar r
GLIBC_DUMP?=$(TC_PATH)/$(OSNAME)_x86_glibc/x86_64-nacl/bin/objdump
GLIBC_PATHS:=-L $(TC_PATH)/$(OSNAME)_x86_glibc/x86_64-nacl/lib32
GLIBC_PATHS+=-L $(TC_PATH)/$(OSNAME)_x86_glibc/x86_64-nacl/lib
GLIBC_CCFLAGS?=-MMD -pthread $(NACL_WARNINGS) -idirafter $(NACL_SDK_ROOT)/include
GLIBC_LDFLAGS?=-pthread
"""

PNACL_DEFAULTS = """
PNACL_CC?=$(TC_PATH)/$(OSNAME)_x86_pnacl/newlib/bin/pnacl-clang -c
PNACL_CXX?=$(TC_PATH)/$(OSNAME)_x86_pnacl/newlib/bin/pnacl-clang++ -c -std=gnu++98
PNACL_LINK?=$(TC_PATH)/$(OSNAME)_x86_pnacl/newlib/bin/pnacl-clang++
PNACL_LIB?=$(TC_PATH)/$(OSNAME)_x86_pnacl/newlib/bin/pnacl-ar r
PNACL_DUMP?=$(TC_PATH)/$(OSNAME)_x86_pnacl/newlib/bin/objdump
PNACL_CCFLAGS?=-MMD -pthread $(NACL_WARNINGS) -idirafter $(NACL_SDK_ROOT)/include
PNACL_LDFLAGS?=-pthread
TRANSLATE:=$(TC_PATH)/$(OSNAME)_x86_pnacl/newlib/bin/pnacl-translate
"""

WIN_DEFAULTS = """
WIN_CC?=cl.exe /nologo
WIN_CXX?=cl.exe /nologo
WIN_LINK?=link.exe /nologo
WIN_LIB?=lib.exe /nologo
WIN_CCFLAGS=/I$(NACL_SDK_ROOT)/include /I$(NACL_SDK_ROOT)/include/win -D WIN32 -D _WIN32
"""

#
# Compile rules for various platforms.
#
CC_RULE = '<tc>/<config>/<name>_<ARCH>.o : %.<ext> $(THIS_MAKE) | <tc>/<config>'
NACL_CC_RULES = {
  'Debug': '<TAB>$(<CC>) -o $@ $< -g -O0 <MACH> -DTCNAME=<tc> $(<TC>_CCFLAGS) $(<PROJ>_<EXT>FLAGS) <DEFLIST> <INCLIST>',
  'Release': '<TAB>$(<CC>) -o $@ $< -O2 <MACH> -DTCNAME=<tc> $(<TC>_CCFLAGS) $(<PROJ>_<EXT>FLAGS) <DEFLIST> <INCLIST>',
}

SO_CC_RULES = {
  'Debug': '<TAB>$(<CC>) -o $@ $< -g -O0 <MACH> -fPIC -DTCNAME=<tc> $(<TC>_CCFLAGS) $(<PROJ>_<EXT>FLAGS) <DEFLIST> <INCLIST>',
  'Release': '<TAB>$(<CC>) -o $@ $< -02 <MACH> -fPIC -DTCNAME=<tc> $(<TC>_CCFLAGS) $(<PROJ>_<EXT>FLAGS) <DEFLIST> <INCLIST>'
}

WIN_CC_RULES = {
  'Debug': '<TAB>$(<CC>) /Od /Fo$@ /MTd /Zi /c $< -DTCNAME=host $(WIN_CCFLAGS) <DEFLIST> <INCLIST>',
  'Release': '<TAB>$(<CC>) /O2 /Fo$@ /MT /c $< -DTCNAME=host $(WIN_CCFLAGS) <DEFLIST> <INCLIST>'
}

#
# Link rules for various platforms.
#
NEXE_LINK_RULES = {
  'Debug': '<TAB>$(<LINK>) -o $@ $^ -g <MACH> $(<TC>_LDFLAGS) $(<PROJ>_LDFLAGS) -L$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config> <LIBLIST>',
  'Release': '<TAB>$(<LINK>) -o $@ $^ <MACH> $(<TC>_LDFLAGS) $(<PROJ>_LDFLAGS) -L$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config> <LIBLIST>'
}

SO_LINK_RULES = {
  'Debug': '<TAB>$(<LINK>) -o $@ $^ -g <MACH> -shared $(<PROJ>_LDFLAGS) -L$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config> <LIBLIST>',
  'Release': '<TAB>$(<LINK>) -o $@ $^ <MACH> -shared $(<PROJ>_LDFLAGS) -L$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config> <LIBLIST>',
}

PEXE_TRANSLATE_RULE = """
<tc>/<config>/<proj>_x86_32.nexe : <tc>/<config>/<proj>.pexe
<TAB>$(TRANSLATE) -arch x86-32 $< -o $@

<tc>/<config>/<proj>_x86_64.nexe : <tc>/<config>/<proj>.pexe
<TAB>$(TRANSLATE) -arch x86-64 $< -o $@

<tc>/<config>/<proj>_arm.nexe : <tc>/<config>/<proj>.pexe
<TAB>$(TRANSLATE) -arch arm $< -o $@"""

PEXE_LINK_RULES = {
  'Debug': '<TAB>$(<LINK>) -o $@ $^ -g $(<TC>_LDFLAGS) $(<PROJ>_LDFLAGS) -L$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config> <LIBLIST>\n' + PEXE_TRANSLATE_RULE,
  'Release': '<TAB>$(<LINK>) -o $@ $^ $(<TC>_LDFLAGS) $(<PROJ>_LDFLAGS) -L$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config> <LIBLIST>\n' + PEXE_TRANSLATE_RULE,
}

WIN_LINK_RULES = {
  'Debug': '<TAB>$(<LINK>) /DLL /OUT:$@ /PDG:$@.pdb /Zi $(<PROJ>_LDFLAGS) /DEBUG /LIBPATH:$(NACL_SDK_ROOT)/lib/win_x86_32_host/Debug $^ <LIBLIST> $(WIN_LDFLAGS)',
  'Release': '<TAB>$(<LINK>) /DLL /OUT:$@ $(<PROJ>_LDFLAGS) /LIBPATH:$(NACL_SDK_ROOT)/lib/win_x86_32_host/Release $^ <LIBLIST> $(WIN_LDFLAGS)'
}

WIN_LAUNCH_RULES = """
HOST_ARGS:=--register-pepper-plugins=$(abspath win/<proj>.dll);application/x-nacl
LAUNCH_HOST: CHECK_FOR_CHROME all
<TAB>$(CHROME_PATH) $(HOST_ARGS) "localhost:5103/index_win.html"
"""

#
# Lib rules for various platforms.
#
POSIX_LIB_RULES = {
  'Debug':
      '<TAB>$(MKDIR) -p $(dir $@)\n'
      '<TAB>$(<LIB>) $@ $^',
  'Release':
      '<TAB>$(MKDIR) -p $(dir $@)\n'
      '<TAB>$(<LIB>) $@ $^',
}

WIN_LIB_RULES = {
  'Debug': '<TAB>$(<LIB>) /OUT:$@ $^ $(WIN_LDFLAGS) <LIBLIST>',
  'Release': '<TAB>$(<LIB>) /OUT:$@ $^ $(WIN_LDFLAGS) <LIBLIST>'
}


#
# NMF rules for various platforms.
#
NMF_RULE = """
<tc>/<config>/<proj>.nmf : <NMF_TARGETS>
<TAB>$(NMF) -D $(<DUMP>) -o $@ $^ -t <tc> -s <tc>/<config>
"""

NMF_EMPTY = """
<tc>/<config>/<proj>.nmf : <NMF_TARGETS> | <tc>/<config>
<TAB>echo {} > $@
"""

GLIBC_NMF_RULE = """
<tc>/<config>/<proj>.nmf : <NMF_TARGETS>
<TAB>$(NMF) -D $(<DUMP>) -o $@ $(GLIBC_PATHS) $^ -t <tc> -s <tc>/<config> $(GLIBC_REMAP)
"""

EXT_MAP = {
  'c': 'CC',
  'cc': 'CXX'
}

WIN_TOOL = {
  'DEFINE': '-D%s',
  'INCLUDE': '/I%s',
  'LIBRARY': '%s.lib',
  'MAIN': '<tc>/<config>/<proj>.dll',
  'NMFMAIN': '<tc>/<config>/<proj>.dll',
  'SO': None,
  'LIB': '$(NACL_SDK_ROOT)/lib/win_<ARCH>_host/<config>/<proj>.lib',
}

NACL_TOOL = {
  'DEFINE': '-D%s',
  'INCLUDE': '-I%s',
  'LIBRARY': '-l%s',
  'MAIN': '<tc>/<config>/<proj>_<ARCH>.nexe',
  'NMFMAIN': '<tc>/<config>/<proj>_<ARCH>.nexe',
  'SO': '<tc>/<config>/<proj>_<ARCH>.so',
  'LIB': '$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config>/lib<proj>.a',
}

PNACL_TOOL = {
  'DEFINE': '-D%s',
  'INCLUDE': '-I%s',
  'LIBRARY': '-l%s',
  'MAIN': '<tc>/<config>/<proj>.pexe',
  'NMFMAIN':
      '<tc>/<config>/<proj>_x86_32.nexe '
      '<tc>/<config>/<proj>_x86_64.nexe '
      '<tc>/<config>/<proj>_arm.nexe',
  'SO': None,
  'LIB': '$(NACL_SDK_ROOT)/lib/$(OSNAME)_<ARCH>_<tc>/<config>/lib<proj>.a',
}


#
# Various Architectures
#
NACL_X86_32 = {
  '<arch>': '32',
  '<ARCH>': 'x86_32',
  '<MACH>': '-m32',
}
NACL_X86_64 = {
  '<arch>': '64',
  '<ARCH>': 'x86_64',
  '<MACH>': '-m64',
}
NACL_PNACL = {
  '<arch>': 'pnacl',
  '<ARCH>': 'PNACL',
  '<MACH>': '',
}
WIN_32 = {
  '<arch>': '',
  '<ARCH>': 'x86_32',
  '<MACH>': '',
}


BUILD_RULES = {
  'newlib' : {
    'ARCHES': [NACL_X86_32, NACL_X86_64],
    'DEFS': NEWLIB_DEFAULTS,
    'CC' : NACL_CC_RULES,
    'CXX' : NACL_CC_RULES,
    'NMF' : NMF_RULE,
    'MAIN': NEXE_LINK_RULES,
    'LIB': POSIX_LIB_RULES,
    'SO' : None,
    'TOOL': NACL_TOOL,
  },
  'glibc' : {
    'ARCHES': [NACL_X86_32, NACL_X86_64],
    'DEFS': GLIBC_DEFAULTS,
    'CC': NACL_CC_RULES,
    'CXX': NACL_CC_RULES,
    'NMF' : GLIBC_NMF_RULE,
    'MAIN': NEXE_LINK_RULES,
    'LIB': POSIX_LIB_RULES,
    'SO': SO_LINK_RULES,
    'TOOL': NACL_TOOL,
  },
  'pnacl' : {
    'ARCHES': [NACL_PNACL],
    'DEFS': PNACL_DEFAULTS,
    'CC': NACL_CC_RULES,
    'CXX': NACL_CC_RULES,
    'NMF' : NMF_RULE,
    'MAIN': PEXE_LINK_RULES,
    'LIB': POSIX_LIB_RULES,
    'SO': None,
    'TOOL': PNACL_TOOL
  },
  'win' : {
    'ARCHES': [WIN_32],
    'DEFS': WIN_DEFAULTS,
    'CC': WIN_CC_RULES,
    'CXX': WIN_CC_RULES,
    'NMF' : NMF_EMPTY,
    'MAIN': WIN_LINK_RULES,
    'LIB': WIN_LIB_RULES,
    'SO': None,
    'TOOL': WIN_TOOL
  }
}

class MakeRules(object):
  """MakeRules generates Tool, Config, and Arch dependend makefile settings.

  The MakeRules object generates strings used in the makefile based on the
  current object settings such as toolchain, configuration, architecture...
  It stores settings such as includes, defines, and lists, and converts them
  to the appropriate format whenever the toolchain changes.
  """

  def __init__(self, tc, cfg=None, arch=None):
    self.tc = tc
    self.defines = []
    self.includes = []
    self.libraries = []
    self.vars = {
      '<TAB>': '\t',
    }
    self.SetToolchain(tc)
    if cfg:
      self.SetConfig(cfg)
    if arch:
      self.SetArch(arch)

  def _BuildList(self, key, items):
    pattern = BUILD_RULES[self.tc]['TOOL'][key]
    if pattern and items:
      items = [pattern % item for item in items]
      return ' '.join(items)
    return ''

  def BuildDefaults(self):
    return BUILD_RULES[self.tc]['DEFS']

  def BuildDirectoryRules(self, configs):
    tc = self.tc
    rules = '\n#\n# Rules for %s toolchain\n#\n%s:\n\t$(MKDIR) %s\n' % (
        tc, tc, tc)
    for cfg in configs:
      rules += '%s/%s: | %s\n\t$(MKDIR) %s/%s\n' % (tc, cfg, tc, tc, cfg)

    rules += '\n# Include header dependency files.\n'
    for cfg in configs:
      rules += '-include %s/%s/*.d\n' % (tc, cfg)
    return rules + '\n'

  def BuildCompileRule(self, EXT, src):
    self.vars['<EXT>'] = EXT
    out = '<tc>/<config>/%s_<ARCH>.o : %s $(THIS_MAKE) | <tc>/<config>\n' % (
        os.path.splitext(src)[0], src)
    out+= BUILD_RULES[self.tc][EXT][self.cfg] + '\n\n'
    return self.Replace(out)

  def BuildLinkRule(self):
    target = BUILD_RULES[self.tc]['TOOL'][self.ptype.upper()]
    out = ''
    if self.ptype == 'lib':
      out = 'ALL_TARGETS+=%s\n' % target
    out += target + ' : $(<PROJ>_<TC>_<CONFIG>_<ARCH>_O)\n'
    out += BUILD_RULES[self.tc][self.ptype.upper()][self.cfg] + '\n\n'
    return self.Replace(out)

  def BuildObjectList(self):
    obj_list = self.GetObjectList()
    sub_str = '$(patsubst %%,%s/%s/%%_%s.o,$(%s_OBJS))' % (
        self.tc, self.cfg, self.arch['<ARCH>'], self.project.upper())
    return '%s:=%s\n' % (obj_list, sub_str)

  def GetArches(self):
    return BUILD_RULES[self.tc]['ARCHES']

  def GetObjectList(self):
    return '%s_%s_%s_%s_O' % (self.project.upper(), self.tc.upper(),
                              self.cfg.upper(), self.arch['<ARCH>'])

  def SetArch(self, arch):
    self.arch = arch
    for key in arch:
      self.vars[key] = arch[key]

  def SetConfig(self, config):
    self.cfg = config
    self.vars['<config>'] = config
    self.vars['<CONFIG>'] = config.upper()

  def SetDefines(self, defs):
    self.defines = defs
    self.vars['<DEFLIST>'] = self._BuildList('DEFINE', defs)

  def SetIncludes(self, incs):
    self.includes = incs
    self.vars['<INCLIST>'] = self._BuildList('INCLUDE', incs)

  def SetLibraries(self, libs):
    self.libraries = libs
    self.vars['<LIBLIST>'] = self._BuildList('LIBRARY', libs)

  def SetProject(self, proj, ptype, defs=None, incs=None, libs=None):
    self.project = proj
    self.ptype = ptype
    self.vars['<proj>'] = proj
    self.vars['<PROJ>'] = proj.upper()
    self.SetDefines(defs)
    self.SetIncludes(incs)
    self.SetLibraries(libs)

  def SetSource(self, src):
    self.source = source
    self.vars['<src>'] = src

  def SetToolchain(self, tc):
    TC = tc.upper()
    self.vars['<CC>'] = '%s_CC' % TC
    self.vars['<CXX>'] = '%s_CXX' % TC
    self.vars['<DUMP>'] = '%s_DUMP' % TC
    self.vars['<LIB>'] = '%s_LIB' % TC
    self.vars['<LINK>'] = '%s_LINK' % TC
    self.vars['<tc>'] = tc
    self.vars['<TC>'] = TC
    self.SetDefines(self.defines)
    self.SetIncludes(self.includes)
    self.SetLibraries(self.libraries)

  def SetVars(self, **kwargs):
    # Add other passed in replacements
    for key in kwargs:
      self.vars['<%s>' % key] = kwargs[key]
    self.var_set = kwargs

  def Replace(self, text):
    return Replace(text, self.vars)


def Replace(text, replacements):
  for key in replacements:
    val = replacements[key]
    if val is not None:
      text = text.replace(key, val)
  return text


def SetVar(varname, values):
  if not values:
    return varname + ':=\n'

  line = varname + ':='
  out = ''
  for value in values:
    if len(line) + len(value) > 78:
      out += line[:-1] + '\n'
      line = '%s+=%s ' % (varname, value)
    else:
      line += value + ' '

  if line:
    out += line[:-1] + '\n'
  return out


def GenerateCleanRules(tools, configs):
  rules = '#\n# Target to remove temporary files\n#\n.PHONY: clean\nclean:\n'
  for tc in tools:
    for cfg in configs:
      rules += '\t$(RM) -fr %s/%s\n' % (tc, cfg)
  return rules + '\n'


def GenerateNMFRules(tc, main, dlls, cfg, arches):
  target = BUILD_RULES[tc]['TOOL']['NMFMAIN']
  dll_target = BUILD_RULES[tc]['TOOL']['SO']
  nmf_targets = []

  for arch in arches:
    replace = {
      '<ARCH>' : arch['<ARCH>'],
      '<config>' : cfg,
      '<DUMP>' : '%s_DUMP' % tc.upper(),
      '<TAB>' : '\t',
      '<tc>' : tc
    }
    for dll in dlls:
      replace['<proj>'] = dll
      nmf_targets.append(Replace(dll_target, replace))
    replace['<proj>'] = main
    nmf_targets.append(Replace(target, replace))

  replace['<NMF_TARGETS>'] = ' '.join(nmf_targets)
  rules = Replace(BUILD_RULES[tc]['NMF'], replace)
  return '\nALL_TARGETS+=%s/%s/%s.nmf' % (tc, cfg, main) + rules + '\n'