OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 Creates a library loader (a header and implementation file), | 7 Creates a library loader (a header and implementation file), |
8 which is a wrapper for dlopen or direct linking with given library. | 8 which is a wrapper for dlopen or direct linking with given library. |
9 | 9 |
10 The loader makes it possible to have the same client code for both cases, | 10 The loader makes it possible to have the same client code for both cases, |
(...skipping 13 matching lines...) Expand all Loading... |
24 HEADER_TEMPLATE = """// This is generated file. Do not modify directly. | 24 HEADER_TEMPLATE = """// This is generated file. Do not modify directly. |
25 // Path to the code generator: %(generator_path)s . | 25 // Path to the code generator: %(generator_path)s . |
26 | 26 |
27 #ifndef %(unique_prefix)s | 27 #ifndef %(unique_prefix)s |
28 #define %(unique_prefix)s | 28 #define %(unique_prefix)s |
29 | 29 |
30 %(wrapped_header_include)s | 30 %(wrapped_header_include)s |
31 | 31 |
32 #include <string> | 32 #include <string> |
33 | 33 |
34 #include "base/basictypes.h" | |
35 #include "base/compiler_specific.h" | |
36 #if defined(%(unique_prefix)s_DLOPEN) | |
37 #include "base/native_library.h" | |
38 #endif | |
39 | |
40 class %(class_name)s { | 34 class %(class_name)s { |
41 public: | 35 public: |
42 %(class_name)s(); | 36 %(class_name)s(); |
43 ~%(class_name)s(); | 37 ~%(class_name)s(); |
44 | 38 |
45 bool Load(const std::string& library_name) WARN_UNUSED_RESULT; | 39 bool Load(const std::string& library_name) |
| 40 __attribute__((warn_unused_result)); |
46 | 41 |
47 bool loaded() const { return loaded_; } | 42 bool loaded() const { return loaded_; } |
48 | 43 |
49 %(member_decls)s | 44 %(member_decls)s |
50 | 45 |
51 private: | 46 private: |
52 void CleanUp(bool unload); | 47 void CleanUp(bool unload); |
53 | 48 |
54 #if defined(%(unique_prefix)s_DLOPEN) | 49 #if defined(%(unique_prefix)s_DLOPEN) |
55 base::NativeLibrary library_; | 50 void* library_; |
56 #endif | 51 #endif |
57 | 52 |
58 bool loaded_; | 53 bool loaded_; |
59 | 54 |
60 DISALLOW_COPY_AND_ASSIGN(%(class_name)s); | 55 // Disallow copy constructor and assignment operator. |
| 56 %(class_name)s(const %(class_name)s&); |
| 57 void operator=(const %(class_name)s&); |
61 }; | 58 }; |
62 | 59 |
63 #endif // %(unique_prefix)s | 60 #endif // %(unique_prefix)s |
64 """ | 61 """ |
65 | 62 |
66 | 63 |
67 HEADER_MEMBER_TEMPLATE = """ typeof(&::%(function_name)s) %(function_name)s; | 64 HEADER_MEMBER_TEMPLATE = """ typeof(&::%(function_name)s) %(function_name)s; |
68 """ | 65 """ |
69 | 66 |
70 | 67 |
71 IMPL_TEMPLATE = """// This is generated file. Do not modify directly. | 68 IMPL_TEMPLATE = """// This is generated file. Do not modify directly. |
72 // Path to the code generator: %(generator_path)s . | 69 // Path to the code generator: %(generator_path)s . |
73 | 70 |
74 #include "%(generated_header_name)s" | 71 #include "%(generated_header_name)s" |
75 | 72 |
| 73 #include <dlfcn.h> |
| 74 |
76 // Put these sanity checks here so that they fire at most once | 75 // Put these sanity checks here so that they fire at most once |
77 // (to avoid cluttering the build output). | 76 // (to avoid cluttering the build output). |
78 #if !defined(%(unique_prefix)s_DLOPEN) && !defined(%(unique_prefix)s_DT_NEEDED) | 77 #if !defined(%(unique_prefix)s_DLOPEN) && !defined(%(unique_prefix)s_DT_NEEDED) |
79 #error neither %(unique_prefix)s_DLOPEN nor %(unique_prefix)s_DT_NEEDED defined | 78 #error neither %(unique_prefix)s_DLOPEN nor %(unique_prefix)s_DT_NEEDED defined |
80 #endif | 79 #endif |
81 #if defined(%(unique_prefix)s_DLOPEN) && defined(%(unique_prefix)s_DT_NEEDED) | 80 #if defined(%(unique_prefix)s_DLOPEN) && defined(%(unique_prefix)s_DT_NEEDED) |
82 #error both %(unique_prefix)s_DLOPEN and %(unique_prefix)s_DT_NEEDED defined | 81 #error both %(unique_prefix)s_DLOPEN and %(unique_prefix)s_DT_NEEDED defined |
83 #endif | 82 #endif |
84 | 83 |
85 #include "base/files/file_path.h" | |
86 #include "base/logging.h" | |
87 | |
88 %(class_name)s::%(class_name)s() : loaded_(false) { | 84 %(class_name)s::%(class_name)s() : loaded_(false) { |
89 } | 85 } |
90 | 86 |
91 %(class_name)s::~%(class_name)s() { | 87 %(class_name)s::~%(class_name)s() { |
92 CleanUp(loaded_); | 88 CleanUp(loaded_); |
93 } | 89 } |
94 | 90 |
95 bool %(class_name)s::Load(const std::string& library_name) { | 91 bool %(class_name)s::Load(const std::string& library_name) { |
96 if (loaded_) { | 92 if (loaded_) |
97 NOTREACHED(); | |
98 return false; | 93 return false; |
99 } | |
100 | 94 |
101 #if defined(%(unique_prefix)s_DLOPEN) | 95 #if defined(%(unique_prefix)s_DLOPEN) |
102 library_ = base::LoadNativeLibrary(base::FilePath(library_name), NULL); | 96 library_ = dlopen(library_name.c_str(), RTLD_LAZY); |
103 if (!library_) | 97 if (!library_) |
104 return false; | 98 return false; |
105 #endif | 99 #endif |
106 | 100 |
107 %(member_init)s | 101 %(member_init)s |
108 | 102 |
109 loaded_ = true; | 103 loaded_ = true; |
110 return true; | 104 return true; |
111 } | 105 } |
112 | 106 |
113 void %(class_name)s::CleanUp(bool unload) { | 107 void %(class_name)s::CleanUp(bool unload) { |
114 #if defined(%(unique_prefix)s_DLOPEN) | 108 #if defined(%(unique_prefix)s_DLOPEN) |
115 if (unload) { | 109 if (unload) { |
116 base::UnloadNativeLibrary(library_); | 110 dlclose(library_); |
117 library_ = NULL; | 111 library_ = NULL; |
118 } | 112 } |
119 #endif | 113 #endif |
120 loaded_ = false; | 114 loaded_ = false; |
121 %(member_cleanup)s | 115 %(member_cleanup)s |
122 } | 116 } |
123 """ | 117 """ |
124 | 118 |
125 IMPL_MEMBER_INIT_TEMPLATE = """ | 119 IMPL_MEMBER_INIT_TEMPLATE = """ |
126 #if defined(%(unique_prefix)s_DLOPEN) | 120 #if defined(%(unique_prefix)s_DLOPEN) |
127 %(function_name)s = | 121 %(function_name)s = |
128 reinterpret_cast<typeof(this->%(function_name)s)>( | 122 reinterpret_cast<typeof(this->%(function_name)s)>( |
129 base::GetFunctionPointerFromNativeLibrary( | 123 dlsym(library_, "%(function_name)s")); |
130 library_, "%(function_name)s")); | |
131 #endif | 124 #endif |
132 #if defined(%(unique_prefix)s_DT_NEEDED) | 125 #if defined(%(unique_prefix)s_DT_NEEDED) |
133 %(function_name)s = &::%(function_name)s; | 126 %(function_name)s = &::%(function_name)s; |
134 #endif | 127 #endif |
135 if (!%(function_name)s) { | 128 if (!%(function_name)s) { |
136 CleanUp(true); | 129 CleanUp(true); |
137 return false; | 130 return false; |
138 } | 131 } |
139 """ | 132 """ |
140 | 133 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 impl_file = open(os.path.join(source_tree_root, options.output_cc), 'w') | 240 impl_file = open(os.path.join(source_tree_root, options.output_cc), 'w') |
248 try: | 241 try: |
249 impl_file.write(impl_contents) | 242 impl_file.write(impl_contents) |
250 finally: | 243 finally: |
251 impl_file.close() | 244 impl_file.close() |
252 | 245 |
253 return 0 | 246 return 0 |
254 | 247 |
255 if __name__ == '__main__': | 248 if __name__ == '__main__': |
256 sys.exit(main()) | 249 sys.exit(main()) |
OLD | NEW |