Skip to content

Commit

Permalink
Merge pull request #92 from PipeRift/feature/build-link-cbindings
Browse files Browse the repository at this point in the history
Declare native types & link
  • Loading branch information
muit authored Feb 25, 2023
2 parents b49fb89 + 1c9a154 commit 356d1a9
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 28 deletions.
12 changes: 8 additions & 4 deletions CMake/Util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ function(rift_runtime_module module)
# Override output folder with suffix
set_target_properties(${target}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Bin"
INCLUDES_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Include"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Bin"
INCLUDES_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Include"
)
set_target_properties(${target} PROPERTIES OUTPUT_NAME "${module}")


add_custom_command(TARGET ${target} POST_BUILD COMMAND
${CMAKE_COMMAND} -E remove_directory "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}"
)
add_custom_command(TARGET ${target} POST_BUILD COMMAND
${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/${module}" "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}"
)
Expand Down
44 changes: 29 additions & 15 deletions Libs/Backends/LLVM/Compiler/Src/LLVMBackend/IRGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "LLVMBackend/IRGeneration.h"

#include "Components/CDeclCStruct.h"
#include "LLVMBackend/Components/CIRFunction.h"
#include "LLVMBackend/Components/CIRModule.h"
#include "LLVMBackend/Components/CIRType.h"
Expand Down Expand Up @@ -31,6 +32,8 @@
#include <AST/Utils/Namespaces.h>
#include <AST/Utils/Statements.h>
#include <Compiler/Compiler.h>
#include <Components/CDeclCStatic.h>
#include <Components/CDeclCStruct.h>
#include <llvm/ADT/APInt.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Module.h>
Expand Down Expand Up @@ -371,23 +374,34 @@ namespace rift::compiler::LLVM
TArray<AST::Id> typeIds;
ecs::GetChildren(ast, moduleId, typeIds);
ecs::ExcludeIfNot<AST::CDeclType>(ast, typeIds);
TArray<AST::Id> structIds = ecs::GetIf<AST::CDeclStruct>(ast, typeIds);
TArray<AST::Id> classIds = ecs::GetIf<AST::CDeclClass>(ast, typeIds);
TArray<AST::Id> staticIds = ecs::GetIf<AST::CDeclStatic>(ast, typeIds);

DeclareStructs(gen, ast, structIds);
DeclareStructs(gen, ast, classIds);

TArray<AST::Id> functionIds;
p::ecs::GetChildren(ast, classIds, functionIds);
p::ecs::GetChildren(ast, staticIds, functionIds);
ecs::ExcludeIfNot<AST::CDeclFunction>(ast, functionIds);
DeclareFunctions(gen, ast, functionIds);

DefineStructs(gen, ast, structIds);
DefineStructs(gen, ast, classIds);
{ // Native declarations
TArray<AST::Id> cStructIds = ecs::GetIf<CDeclCStruct>(ast, typeIds);
TArray<AST::Id> cStaticIds = ecs::GetIf<CDeclCStatic>(ast, typeIds);
TArray<AST::Id> cFunctionIds;
p::ecs::GetChildren(ast, cStaticIds, cFunctionIds);
ecs::ExcludeIfNot<AST::CDeclFunction>(ast, cFunctionIds);
DeclareStructs(gen, ast, cStructIds);
DeclareFunctions(gen, ast, cFunctionIds);
}

DefineFunctions(gen, ast, functionIds);
{ // Rift declarations & definitions
TArray<AST::Id> structIds = ecs::GetIf<AST::CDeclStruct>(ast, typeIds);
TArray<AST::Id> classIds = ecs::GetIf<AST::CDeclClass>(ast, typeIds);
TArray<AST::Id> staticIds = ecs::GetIf<AST::CDeclStatic>(ast, typeIds);
TArray<AST::Id> functionIds;
p::ecs::GetChildren(ast, classIds, functionIds);
p::ecs::GetChildren(ast, staticIds, functionIds);
ecs::ExcludeIfNot<AST::CDeclFunction>(ast, functionIds);

DeclareStructs(gen, ast, structIds);
DeclareStructs(gen, ast, classIds);
DeclareFunctions(gen, ast, functionIds);

DefineStructs(gen, ast, structIds);
DefineStructs(gen, ast, classIds);
DefineFunctions(gen, ast, functionIds);
}

if (module.target == AST::RiftModuleTarget::Executable)
{
Expand Down
30 changes: 23 additions & 7 deletions Libs/Backends/LLVM/Compiler/Src/LLVMBackend/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "LLVMBackend/Linker.h"

#include "Components/CNativeBinding.h"
#include "LLVMBackend/Components/CIRModule.h"
#include "Pipe/Core/PlatformProcess.h"
#include "Pipe/Core/String.h"
Expand All @@ -16,14 +17,12 @@
#include <Pipe/Reflect/EnumType.h>



namespace rift::compiler::LLVM
{
void Link(Compiler& compiler)
{
String linkerPath{PlatformProcess::GetExecutablePath()};
linkerPath.append("/");
linkerPath.append(RIFT_LLVM_LINKER_PATH);
String linkerPath{
p::JoinPaths(PlatformProcess::GetExecutablePath(), RIFT_LLVM_LINKER_PATH)};

for (AST::Id moduleId : ecs::ListAll<AST::CModule, CIRModule>(compiler.ast))
{
Expand All @@ -33,7 +32,6 @@ namespace rift::compiler::LLVM
if (p::files::Exists(irModule.objectFile))
{
TArray<const char*> command;

command.Add(linkerPath.c_str());

const char* extension = nullptr;
Expand All @@ -52,6 +50,18 @@ namespace rift::compiler::LLVM
extension = "lib";
break;
}

p::TArray<p::String> binaryPaths;
if (auto* cBinding = compiler.ast.TryGet<CNativeBinding>(moduleId))
{
p::StringView modulePath = AST::GetModulePath(compiler.ast, moduleId);
p::String binaryPath;
for (const auto& nativeBinary : cBinding->binaries)
{
binaryPaths.Add(p::JoinPaths(modulePath, nativeBinary));
command.Add(binaryPaths.Last().c_str());
}
}
command.Add(irModule.objectFile.data());

p::Path filePath =
Expand All @@ -60,8 +70,14 @@ namespace rift::compiler::LLVM
command.Add(outParam.data());

Log::Info("Linking '{}' from '{}'", p::ToString(filePath), irModule.objectFile);
p::RunProcess(command,
SubprocessOptions::TerminateIfDestroyed | SubprocessOptions::CombinedOutErr);
auto process = p::RunProcess(command,
SubprocessOptions::TerminateIfDestroyed | SubprocessOptions::CombinedOutErr);
i32 returnCode = 0;
p::WaitProcess(process.TryGet(), &returnCode);
if (returnCode != 0)
{
compiler.AddError("Linking failed");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace rift
STRUCT(CNativeBinding, p::Struct)

PROP(binaries)
p::TArray<p::Path> binaries;
p::TArray<p::String> binaries;

PROP(autoGenerateDefinitions)
bool autoGenerateDefinitions = false;
};
} // namespace rift
5 changes: 5 additions & 0 deletions Libs/Bindings/Native/Compiler/Src/NativeBindingModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ namespace rift
TArray<AST::Id> moduleIds;
p::ecs::ListAll<AST::CModule, CNativeBinding>(ast, moduleIds);

// Only use automatic native bindings on modules marked as such
moduleIds.RemoveIfSwap([ast](auto id) {
return !ast.Get<CNativeBinding>(id).autoGenerateDefinitions;
});

TArray<ParsedModule> parsedModules;
parsedModules.Reserve(moduleIds.Size());
for (i32 i = 0; i < moduleIds.Size(); ++i)
Expand Down
2 changes: 1 addition & 1 deletion Libs/Pipe
1 change: 1 addition & 0 deletions Libs/Runtimes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/Lib/*
1 change: 1 addition & 0 deletions Libs/Runtimes/IO/__module__.rf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"CModule": {
"target": "Static",
"dependencies": []
},
"CNativeBinding": {
Expand Down

0 comments on commit 356d1a9

Please sign in to comment.