Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2971)

Unified Diff: chrome/imports/build_import_libraries.py

Issue 12210017: Start of build config for custom user32 import libs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Implement the delay load hook. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/imports/build_import_libraries.py
diff --git a/chrome/imports/build_import_libraries.py b/chrome/imports/build_import_libraries.py
new file mode 100644
index 0000000000000000000000000000000000000000..f89537e8ece70bc32427cb55d171bb7c5e4be697
--- /dev/null
+++ b/chrome/imports/build_import_libraries.py
@@ -0,0 +1,212 @@
+#!/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.
+#
+# A utility script to create minimal import libs from an import description
M-A Ruel 2013/02/07 21:14:30 Make that a docstring
+# file.
+import ast
+import logging
+import optparse
+import os
+import os.path
+import shutil
+import subprocess
+import sys
+import tempfile
+
+
+_USAGE = """\
+Usage: %prog [options] [imports-file]
+
+Creates a pair of import libraries from imports-file.
M-A Ruel 2013/02/07 21:14:30 Often, I make this the docstring and then use: op
+
+Note: this script uses the microsoft assembler (ml.exe) and the library tool
+ (lib.exe), both of which must be in path.
+"""
+
+
+_ASM_STUB_HEADER = """\
+; This file is autogenerated, do not edit.
+.386
+.MODEL FLAT, C
+.CODE
+
+; Stubs to provide mangled names to lib.exe for the
+; correct generation of import libs.
+"""
+
+
+_DEF_STUB_HEADER = """\
+; This file is autogenerated, do not edit.
+
+; Export declarations for generating import libs.
+"""
+
+
+_LOGGER = logging.getLogger()
+
+
+
+class _Error(Exception):
+ pass
+
+
+def _Shell(cmd, **kw):
+ ret = subprocess.call(cmd, **kw)
+ _LOGGER.info('Running "%s" returned %d.', cmd, ret)
+ if ret != 0:
+ raise _Error('Command "%s" returned %d.' % (cmd, ret))
+
+
+def _ReadImportsFile(imports_file):
+ # Slurp the imports file.
+ return ast.literal_eval(open(imports_file).read())
+
+
+def _WriteStubsFile(import_names, output_file):
+ output_file.write(_ASM_STUB_HEADER)
+
+ for name in import_names:
+ output_file.write('%s PROC\n' % name)
+ output_file.write('%s ENDP\n' % name)
+
+ output_file.write('END\n')
+
+
+def _WriteDefFile(dll_name, import_names, output_file):
+ output_file.write(_DEF_STUB_HEADER)
+ output_file.write('NAME %s\n' % dll_name)
+ output_file.write('EXPORTS\n')
+ for name in import_names:
+ name = name.split('@')[0]
+ output_file.write(' %s\n' % name)
+
+
+def _CreateImportLib(dll_name, imports, temp_dir, output_file):
+ # For each library write an assmbly file containing empty declarations
M-A Ruel 2013/02/07 21:14:30 assembly And I'd make the whole thing a docstring
+ # for each imported function of the form:
+ #
+ # AddClipboardFormatListener@4 PROC
+ # AddClipboardFormatListener@4 ENDP
+ #
+ # The resulting object file is then supplied to lib.exe with a .def file
+ # declaring the corresponding non-adorned exports as they appear on the
+ # exporting DLL, e.g.
+ #
+ # EXPORTS
+ # AddClipboardFormatListener
+ #
+ # In combination, these two things cause lib.exe to generate an import lib
+ # with public symbols named like "__imp__AddClipboardFormatListener@4",
+ # binding to names like "AddClipboardFormatListener".
+ #
+ # All of this is perpetrated in a temporary directory, as the intermediate
+ # artifacts are quick and easy to produce, and of no interest to anyone
+ # after the fact.
+
+ # Create an .asm file to provide stdcall-like stub names to lib.exe.
+ asm_name = dll_name + '.asm'
+ _LOGGER.info('Writing asm file "%s".', asm_name)
+ with open(os.path.join(temp_dir, asm_name), 'w') as stubs_file:
+ _WriteStubsFile(imports, stubs_file)
+
+ # Invoke on the assembler to compile it to .obj.
+ obj_name = dll_name + '.obj'
+ cmdline = ['ml.exe', '/nologo', '/c', asm_name, '/Fo', obj_name]
+ _Shell(cmdline, cwd=temp_dir)
+
+ # Create the corresponding .def file. This file has the non stdcall-adorned
+ # names, that need to correlate to the ASM above.
+ def_name = dll_name + '.def'
+ _LOGGER.info('Writing def file "%s".', def_name)
+ with open(os.path.join(temp_dir, def_name), 'w') as def_file:
+ _WriteDefFile(dll_name, imports, def_file)
+
+ # Invoke on lib.exe to create the import library.
+ # We generate everything into the temporary directory, as the .exp export
+ # files will be generated at the same path as the import library, and we
+ # don't want those files potentially gunking the works.
+ dll_base_name, ext = os.path.splitext(dll_name)
+ lib_name = dll_base_name + '.lib'
+ cmdline = ['lib.exe',
+ obj_name,
+ '/def:%s' % def_name,
+ '/out:%s' % lib_name]
+
+ _Shell(cmdline, cwd=temp_dir)
+
+ # Copy the .lib file to the output directory.
+ shutil.copyfile(os.path.join(temp_dir, lib_name), output_file)
+ _LOGGER.info('Created "%s".', output_file)
+
+
+def _CreateImportLibs(imports, temp_dir, output_dir):
+ dll_name = imports['dll_name']
+ dll_base_name, ext = os.path.splitext(dll_name)
+
+ # Creates an import library for the hard imports named DLL-imports.lib,
+ # e.g. user32-imports.lib. This import library will bind to dll_name.
+ _CreateImportLib(dll_name,
+ imports['imports'],
+ temp_dir,
+ os.path.join(output_dir, dll_base_name + '-imports.lib'))
+ # Creates an import library for the delay imports named DLL-delay.lib,
+ # e.g. user32-delay.lib. This import library will bind to dll_name-delay.dll,
+ # e.g. user32-delay.dll.
+ _CreateImportLib(dll_base_name + '-delay' + ext,
+ imports['delay_imports'],
+ temp_dir,
+ os.path.join(output_dir, dll_base_name + '-delay.lib'))
+
+
+def main():
+ parser = optparse.OptionParser(usage=_USAGE)
+ parser.add_option('-o', '--output-dir',
+ dest='output_dir',
+ help='Specifies the output directory.')
+ parser.add_option('-k', '--keep-temp-dir',
+ dest='keep_temp_dir',
+ action='store_true',
+ default=False,
+ help='Keep the temporary directory.')
+ parser.add_option('-v', '--verbose',
+ dest='verbose',
+ action='store_true',
+ default=False,
+ help='Verbose logging.')
+
+ options, args = parser.parse_args()
+ if len(args) != 1:
+ parser.error('Must provide an imports file.')
+
+ if not options.output_dir:
+ parser.error('Must provide an output directory.')
+
+ options.output_dir = os.path.abspath(options.output_dir)
+
+ if options.verbose:
+ logging.basicConfig(level=logging.INFO)
+ else:
+ logging.basicConfig(level=logging.WARN)
+
+ # Read the imports file.
+ imports = _ReadImportsFile(args[0])
+
+ temp_dir = tempfile.mkdtemp()
+ _LOGGER.info('Created temporary directory "%s."', temp_dir)
+ try:
+ ret = _CreateImportLibs(imports, temp_dir, options.output_dir)
+ except Exception, e:
+ _LOGGER.exception('Failed to create imports.')
+ ret = 1
+ finally:
+ if not options.keep_temp_dir:
+ shutil.rmtree(temp_dir)
+ _LOGGER.info('Deleted temporary directory "%s."', temp_dir)
+
+ return ret
+
+
+if __name__ == '__main__':
+ sys.exit(main())

Powered by Google App Engine
This is Rietveld 408576698