Skip to content

Commit 89422a4

Browse files
committed
Add force link macro
* It could help us get rid of --whole-archieve. * Add comments
1 parent 495649a commit 89422a4

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

paddle/utils/ForceLink.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
/// Declare a force link file ID. It can be enabled by
18+
/// `PADDLE_ENABLE_FORCE_LINK_FILE`. It is
19+
///
20+
/// Example:
21+
///
22+
/// In some_file.cpp
23+
/// @code{cpp}
24+
/// static paddle::InitFunction init([]{...});
25+
/// PADDLE_REGISTER_FORCE_LINK_FILE(some_file)
26+
/// @endcode{cpp}
27+
///
28+
/// In main.cpp
29+
/// @code{cpp}
30+
/// PADDLE_ENABLE_FORCE_LINK_FILE(some_file);
31+
///
32+
/// int main() {
33+
/// ...
34+
/// }
35+
/// @endcode{cpp}
36+
///
37+
/// Then the InitFunction in some_file.cpp can be invoked.
38+
#define PADDLE_REGISTER_FORCE_LINK_FILE(ID) \
39+
int __paddle_register_force_link_file_##ID##_method__() { return 0; }
40+
41+
/// Enable a force link file. The file with ID's static variables could
42+
/// be all initialized.
43+
#define PADDLE_ENABLE_FORCE_LINK_FILE(ID) \
44+
extern int __paddle_register_force_link_file_##ID##_method__(); \
45+
static int __paddle_register_force_link_file_##ID##_handler__ = \
46+
__paddle_register_force_link_file_##ID##_method__();

paddle/utils/tests/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ if(NOT APPLE)
1515
COMMAND ${PROJ_ROOT}/paddle/utils/tests/test_CustomStackTracePrint.sh
1616
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1717
endif()
18+
19+
add_library(test_class_registrar_lib STATIC
20+
test_ClassRegistrarLib.cpp
21+
test_ClassRegistrarGlobals.cpp)
22+
23+
add_simple_unittest(test_ClassRegistrar)
24+
target_link_libraries(test_ClassRegistrar
25+
test_class_registrar_lib)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <gtest/gtest.h>
16+
#include <paddle/utils/ForceLink.h>
17+
#include "test_ClassRegistrarLib.h"
18+
// Enable link test_ClassRegistrarLib.cpp
19+
PADDLE_ENABLE_FORCE_LINK_FILE(test_registrar);
20+
21+
TEST(ClassRegistrar, test) {
22+
std::vector<std::string> types;
23+
gTestRegistrar_.forEachType(
24+
[&types](const std::string& tp) { types.push_back(tp); });
25+
ASSERT_EQ(1, types.size());
26+
ASSERT_EQ("test", types[0]);
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "test_ClassRegistrarLib.h"
16+
paddle::ClassRegistrar<BaseClass> gTestRegistrar_;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "test_ClassRegistrarLib.h"
16+
#include <paddle/utils/ForceLink.h>
17+
BaseClass::~BaseClass() {}
18+
19+
class TestRegistrar : public BaseClass {
20+
public:
21+
TestRegistrar() {}
22+
23+
virtual ~TestRegistrar() {}
24+
};
25+
26+
static paddle::InitFunction init([] {
27+
gTestRegistrar_.registerClass(
28+
"test", []() -> BaseClass* { return new TestRegistrar(); });
29+
});
30+
31+
PADDLE_REGISTER_FORCE_LINK_FILE(test_registrar);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
#include "paddle/utils/ClassRegistrar.h"
17+
18+
class BaseClass {
19+
public:
20+
virtual ~BaseClass();
21+
};
22+
23+
extern paddle::ClassRegistrar<BaseClass> gTestRegistrar_;

0 commit comments

Comments
 (0)