Skip to content

Commit

Permalink
Add force link macro
Browse files Browse the repository at this point in the history
* It could help us get rid of --whole-archieve.
* Add comments
  • Loading branch information
reyoung committed Jan 3, 2017
1 parent 495649a commit 89422a4
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
46 changes: 46 additions & 0 deletions paddle/utils/ForceLink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

/// Declare a force link file ID. It can be enabled by
/// `PADDLE_ENABLE_FORCE_LINK_FILE`. It is
///
/// Example:
///
/// In some_file.cpp
/// @code{cpp}
/// static paddle::InitFunction init([]{...});
/// PADDLE_REGISTER_FORCE_LINK_FILE(some_file)
/// @endcode{cpp}
///
/// In main.cpp
/// @code{cpp}
/// PADDLE_ENABLE_FORCE_LINK_FILE(some_file);
///
/// int main() {
/// ...
/// }
/// @endcode{cpp}
///
/// Then the InitFunction in some_file.cpp can be invoked.
#define PADDLE_REGISTER_FORCE_LINK_FILE(ID) \
int __paddle_register_force_link_file_##ID##_method__() { return 0; }

/// Enable a force link file. The file with ID's static variables could
/// be all initialized.
#define PADDLE_ENABLE_FORCE_LINK_FILE(ID) \
extern int __paddle_register_force_link_file_##ID##_method__(); \
static int __paddle_register_force_link_file_##ID##_handler__ = \
__paddle_register_force_link_file_##ID##_method__();
8 changes: 8 additions & 0 deletions paddle/utils/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ if(NOT APPLE)
COMMAND ${PROJ_ROOT}/paddle/utils/tests/test_CustomStackTracePrint.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endif()

add_library(test_class_registrar_lib STATIC
test_ClassRegistrarLib.cpp
test_ClassRegistrarGlobals.cpp)

add_simple_unittest(test_ClassRegistrar)
target_link_libraries(test_ClassRegistrar
test_class_registrar_lib)
27 changes: 27 additions & 0 deletions paddle/utils/tests/test_ClassRegistrar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <gtest/gtest.h>
#include <paddle/utils/ForceLink.h>
#include "test_ClassRegistrarLib.h"
// Enable link test_ClassRegistrarLib.cpp
PADDLE_ENABLE_FORCE_LINK_FILE(test_registrar);

TEST(ClassRegistrar, test) {
std::vector<std::string> types;
gTestRegistrar_.forEachType(
[&types](const std::string& tp) { types.push_back(tp); });
ASSERT_EQ(1, types.size());
ASSERT_EQ("test", types[0]);
}
16 changes: 16 additions & 0 deletions paddle/utils/tests/test_ClassRegistrarGlobals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "test_ClassRegistrarLib.h"
paddle::ClassRegistrar<BaseClass> gTestRegistrar_;
31 changes: 31 additions & 0 deletions paddle/utils/tests/test_ClassRegistrarLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "test_ClassRegistrarLib.h"
#include <paddle/utils/ForceLink.h>
BaseClass::~BaseClass() {}

class TestRegistrar : public BaseClass {
public:
TestRegistrar() {}

virtual ~TestRegistrar() {}
};

static paddle::InitFunction init([] {
gTestRegistrar_.registerClass(
"test", []() -> BaseClass* { return new TestRegistrar(); });
});

PADDLE_REGISTER_FORCE_LINK_FILE(test_registrar);
23 changes: 23 additions & 0 deletions paddle/utils/tests/test_ClassRegistrarLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
#include "paddle/utils/ClassRegistrar.h"

class BaseClass {
public:
virtual ~BaseClass();
};

extern paddle::ClassRegistrar<BaseClass> gTestRegistrar_;

0 comments on commit 89422a4

Please sign in to comment.