1#!/usr/bin/env python
2
3# Copyright (c) 2012 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# This script takes libcmt.lib for VS2013 and removes the allocation related
8# functions from it.
9#
10# Usage: prep_libc.py <VCLibDir> <OutputDir> <arch> [<environment_file>]
11#
12# VCLibDir is the path where VC is installed, something like:
13#    C:\Program Files\Microsoft Visual Studio 8\VC\lib
14#
15# OutputDir is the directory where the modified libcmt file should be stored.
16# arch is one of: 'ia32', 'x86' or 'x64'. ia32 and x86 are synonyms.
17#
18# If the environment_file argument is set, the environment variables in the
19# given file will be used to execute the VC tools. This file is in the same
20# format as the environment block passed to CreateProcess.
21
22import os
23import shutil
24import subprocess
25import sys
26
27def run(command, env_dict):
28  """Run |command|.  If any lines that match an error condition then
29      terminate.
30
31  The env_dict, will be used for the environment. None can be used to get the
32  default environment."""
33  error = 'cannot find member object'
34  # Need shell=True to search the path in env_dict for the executable.
35  popen = subprocess.Popen(
36      command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True,
37      env=env_dict)
38  out, _ = popen.communicate()
39  for line in out.splitlines():
40    print line
41    if error and line.find(error) != -1:
42      print 'prep_libc.py: Error stripping object from C runtime.'
43      sys.exit(1)
44
45def main():
46  bindir = 'SELF_X86'
47  objdir = 'INTEL'
48  vs_install_dir = sys.argv[1]
49  outdir = sys.argv[2]
50  if "x64" in sys.argv[3]:
51    bindir = 'SELF_64_amd64'
52    objdir = 'amd64'
53    vs_install_dir = os.path.join(vs_install_dir, 'amd64')
54
55  if len(sys.argv) == 5:
56    env_pairs = open(sys.argv[4]).read()[:-2].split('\0')
57    env_dict = dict([item.split('=', 1) for item in env_pairs])
58  else:
59    env_dict = None  # Use the default environment.
60
61  output_lib = os.path.join(outdir, 'libcmt.lib')
62  shutil.copyfile(os.path.join(vs_install_dir, 'libcmt.lib'), output_lib)
63  shutil.copyfile(os.path.join(vs_install_dir, 'libcmt.pdb'),
64                  os.path.join(outdir, 'libcmt.pdb'))
65  cvspath = 'f:\\binaries\\Intermediate\\vctools\\crt_bld\\' + bindir + \
66      '\\crt\\prebuild\\build\\' + objdir + '\\mt_obj\\nativec\\\\';
67  cppvspath = 'f:\\binaries\\Intermediate\\vctools\\crt_bld\\' + bindir + \
68      '\\crt\\prebuild\\build\\' + objdir + '\\mt_obj\\nativecpp\\\\';
69
70  cobjfiles = ['malloc', 'free', 'realloc', 'heapinit', 'calloc', 'recalloc',
71      'calloc_impl']
72  cppobjfiles = ['new', 'new2', 'delete', 'delete2', 'new_mode', 'newopnt',
73      'newaopnt']
74  for obj in cobjfiles:
75    cmd = ('lib /nologo /ignore:4006,4221 /remove:%s%s.obj %s' %
76           (cvspath, obj, output_lib))
77    run(cmd, env_dict)
78  for obj in cppobjfiles:
79    cmd = ('lib /nologo /ignore:4006,4221 /remove:%s%s.obj %s' %
80           (cppvspath, obj, output_lib))
81    run(cmd, env_dict)
82
83if __name__ == "__main__":
84  sys.exit(main())
85