diff options
author | Yun Peng <pcloudy@google.com> | 2017-08-25 09:59:14 +0200 |
---|---|---|
committer | Yun Peng <pcloudy@google.com> | 2017-08-28 09:37:47 +0200 |
commit | 66437a0173488ca4e77aab8c41d5454871c8c6a2 (patch) | |
tree | 3527d0aff2c40c5be751aa972d880b27e6eb6aa2 /third_party/def_parser/def_parser_main.cc | |
parent | 9e3a3e2f6a16288febc74a4a3c9086c995f6aa5a (diff) |
Windows: Implementing C++ DEF parser
C++ DEF parser can generating a DEF file from a object file, which can
be used to export symbols during linking DLL on Windows.
This parser is based on an implementation in CMake
See https://github.com/Kitware/CMake/blob/master/Source/bindexplib.cxx
A few changes has been made to make it work better.
Usage: output_deffile dllname [objfile ...] [input_deffile ...] [@paramfile ...]
output_deffile: the output DEF file
dllname: the DLL name this DEF file is used for, if dllname is not empty
string (eg. ""), def_parser writes an 'LIBRARY <dllname>' entry
into DEF file.
objfile: a object file, def_parser parses this file to find symbols,
then merges them into final result.
Can apppear multiple times.
input_deffile: an existing def file, def_parser merges all symbols in this file.
Can appear multiple times.
@paramfile: a parameter file that can contain objfile and input_deffile
Can appear multiple time.
Change-Id: I0ee65fa3119ecae2ea195b707af5690e4bc6a6c2
Diffstat (limited to 'third_party/def_parser/def_parser_main.cc')
-rw-r--r-- | third_party/def_parser/def_parser_main.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/third_party/def_parser/def_parser_main.cc b/third_party/def_parser/def_parser_main.cc new file mode 100644 index 0000000000..a92f0e9aa0 --- /dev/null +++ b/third_party/def_parser/def_parser_main.cc @@ -0,0 +1,77 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ + +#include <fstream> +#include <iostream> +#include <string> + +#include "third_party/def_parser/def_parser.h" + +static const char* ws = " \t\n\r\f\v"; + +inline void trim(std::string *str) { + // Remove prefixing spaces + str->erase(0, str->find_first_not_of(ws)); + // Remove suffixing spaces + str->erase(str->find_last_not_of(ws) + 1); +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + std::cerr << "Usage: output_def_file dllname [objfile ...] [input_deffile ...] [@paramfile ...]\n"; + std::cerr << "output_deffile: the output DEF file\n"; + std::cerr << "\n"; + std::cerr << "dllname: the DLL name this DEF file is used for, if dllname is not empty\n"; + std::cerr << " string (eg. ""), def_parser writes an 'LIBRARY <dllname>' entry\n"; + std::cerr << " into DEF file.\n"; + std::cerr << "\n"; + std::cerr << "objfile: a object file, def_parser parses this file to find symbols,\n"; + std::cerr << " then merges them into final result.\n"; + std::cerr << " Can apppear multiple times.\n"; + std::cerr << "\n"; + std::cerr << "input_deffile: an existing def file, def_parser merges all symbols in this file.\n"; + std::cerr << " Can appear multiple times.\n"; + std::cerr << "\n"; + std::cerr << "@paramfile: a parameter file that can contain objfile and input_deffile.\n"; + std::cerr << " Can appear multiple time.\n"; + return 1; + } + + FILE* fout = fopen(argv[1], "w+"); + if (!fout) { + std::cerr << "Could not open output .def file: " << argv[1] << "\n"; + return 1; + } + + DefParser deffile; + + deffile.SetDLLName(argv[2]); + + for (int i = 3; i < argc; i++) { + // If the argument starts with @, then treat it as a parameter file. + if (argv[i][0] == '@') { + std::ifstream paramfile(argv[i] + 1, std::ios::in | std::ios::binary); + if (!paramfile) { + std::cerr << "Could not open parameter file: " << argv[i] << "\n"; + return 1; + } + std::string file; + while (std::getline(paramfile, file)) { + trim(&file); + if (!deffile.AddFile(file)) { + return 1; + } + } + } else { + std::string file(argv[i]); + trim(&file); + if (!deffile.AddFile(argv[i])) { + return 1; + } + } + } + + deffile.WriteFile(fout); + fclose(fout); + return 0; +} |