Skip to content

Commit 356d1a9

Browse files
authored
Merge pull request #92 from PipeRift/feature/build-link-cbindings
Declare native types & link
2 parents b49fb89 + 1c9a154 commit 356d1a9

File tree

8 files changed

+72
-28
lines changed

8 files changed

+72
-28
lines changed

CMake/Util.cmake

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ function(rift_runtime_module module)
3939
# Override output folder with suffix
4040
set_target_properties(${target}
4141
PROPERTIES
42-
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Lib"
43-
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Lib"
44-
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Bin"
45-
INCLUDES_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}/Include"
42+
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Lib"
43+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Lib"
44+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Bin"
45+
INCLUDES_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${module}/Include"
4646
)
4747
set_target_properties(${target} PROPERTIES OUTPUT_NAME "${module}")
4848

49+
50+
add_custom_command(TARGET ${target} POST_BUILD COMMAND
51+
${CMAKE_COMMAND} -E remove_directory "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}"
52+
)
4953
add_custom_command(TARGET ${target} POST_BUILD COMMAND
5054
${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/${module}" "${CMAKE_BINARY_DIR}/Bin/Runtimes/${module}"
5155
)

Libs/Backends/LLVM/Compiler/Src/LLVMBackend/IRGeneration.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "LLVMBackend/IRGeneration.h"
44

5+
#include "Components/CDeclCStruct.h"
56
#include "LLVMBackend/Components/CIRFunction.h"
67
#include "LLVMBackend/Components/CIRModule.h"
78
#include "LLVMBackend/Components/CIRType.h"
@@ -31,6 +32,8 @@
3132
#include <AST/Utils/Namespaces.h>
3233
#include <AST/Utils/Statements.h>
3334
#include <Compiler/Compiler.h>
35+
#include <Components/CDeclCStatic.h>
36+
#include <Components/CDeclCStruct.h>
3437
#include <llvm/ADT/APInt.h>
3538
#include <llvm/IR/Function.h>
3639
#include <llvm/IR/Module.h>
@@ -371,23 +374,34 @@ namespace rift::compiler::LLVM
371374
TArray<AST::Id> typeIds;
372375
ecs::GetChildren(ast, moduleId, typeIds);
373376
ecs::ExcludeIfNot<AST::CDeclType>(ast, typeIds);
374-
TArray<AST::Id> structIds = ecs::GetIf<AST::CDeclStruct>(ast, typeIds);
375-
TArray<AST::Id> classIds = ecs::GetIf<AST::CDeclClass>(ast, typeIds);
376-
TArray<AST::Id> staticIds = ecs::GetIf<AST::CDeclStatic>(ast, typeIds);
377377

378-
DeclareStructs(gen, ast, structIds);
379-
DeclareStructs(gen, ast, classIds);
380-
381-
TArray<AST::Id> functionIds;
382-
p::ecs::GetChildren(ast, classIds, functionIds);
383-
p::ecs::GetChildren(ast, staticIds, functionIds);
384-
ecs::ExcludeIfNot<AST::CDeclFunction>(ast, functionIds);
385-
DeclareFunctions(gen, ast, functionIds);
386-
387-
DefineStructs(gen, ast, structIds);
388-
DefineStructs(gen, ast, classIds);
378+
{ // Native declarations
379+
TArray<AST::Id> cStructIds = ecs::GetIf<CDeclCStruct>(ast, typeIds);
380+
TArray<AST::Id> cStaticIds = ecs::GetIf<CDeclCStatic>(ast, typeIds);
381+
TArray<AST::Id> cFunctionIds;
382+
p::ecs::GetChildren(ast, cStaticIds, cFunctionIds);
383+
ecs::ExcludeIfNot<AST::CDeclFunction>(ast, cFunctionIds);
384+
DeclareStructs(gen, ast, cStructIds);
385+
DeclareFunctions(gen, ast, cFunctionIds);
386+
}
389387

390-
DefineFunctions(gen, ast, functionIds);
388+
{ // Rift declarations & definitions
389+
TArray<AST::Id> structIds = ecs::GetIf<AST::CDeclStruct>(ast, typeIds);
390+
TArray<AST::Id> classIds = ecs::GetIf<AST::CDeclClass>(ast, typeIds);
391+
TArray<AST::Id> staticIds = ecs::GetIf<AST::CDeclStatic>(ast, typeIds);
392+
TArray<AST::Id> functionIds;
393+
p::ecs::GetChildren(ast, classIds, functionIds);
394+
p::ecs::GetChildren(ast, staticIds, functionIds);
395+
ecs::ExcludeIfNot<AST::CDeclFunction>(ast, functionIds);
396+
397+
DeclareStructs(gen, ast, structIds);
398+
DeclareStructs(gen, ast, classIds);
399+
DeclareFunctions(gen, ast, functionIds);
400+
401+
DefineStructs(gen, ast, structIds);
402+
DefineStructs(gen, ast, classIds);
403+
DefineFunctions(gen, ast, functionIds);
404+
}
391405

392406
if (module.target == AST::RiftModuleTarget::Executable)
393407
{

Libs/Backends/LLVM/Compiler/Src/LLVMBackend/Linker.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "LLVMBackend/Linker.h"
44

5+
#include "Components/CNativeBinding.h"
56
#include "LLVMBackend/Components/CIRModule.h"
67
#include "Pipe/Core/PlatformProcess.h"
78
#include "Pipe/Core/String.h"
@@ -16,14 +17,12 @@
1617
#include <Pipe/Reflect/EnumType.h>
1718

1819

19-
2020
namespace rift::compiler::LLVM
2121
{
2222
void Link(Compiler& compiler)
2323
{
24-
String linkerPath{PlatformProcess::GetExecutablePath()};
25-
linkerPath.append("/");
26-
linkerPath.append(RIFT_LLVM_LINKER_PATH);
24+
String linkerPath{
25+
p::JoinPaths(PlatformProcess::GetExecutablePath(), RIFT_LLVM_LINKER_PATH)};
2726

2827
for (AST::Id moduleId : ecs::ListAll<AST::CModule, CIRModule>(compiler.ast))
2928
{
@@ -33,7 +32,6 @@ namespace rift::compiler::LLVM
3332
if (p::files::Exists(irModule.objectFile))
3433
{
3534
TArray<const char*> command;
36-
3735
command.Add(linkerPath.c_str());
3836

3937
const char* extension = nullptr;
@@ -52,6 +50,18 @@ namespace rift::compiler::LLVM
5250
extension = "lib";
5351
break;
5452
}
53+
54+
p::TArray<p::String> binaryPaths;
55+
if (auto* cBinding = compiler.ast.TryGet<CNativeBinding>(moduleId))
56+
{
57+
p::StringView modulePath = AST::GetModulePath(compiler.ast, moduleId);
58+
p::String binaryPath;
59+
for (const auto& nativeBinary : cBinding->binaries)
60+
{
61+
binaryPaths.Add(p::JoinPaths(modulePath, nativeBinary));
62+
command.Add(binaryPaths.Last().c_str());
63+
}
64+
}
5565
command.Add(irModule.objectFile.data());
5666

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

6272
Log::Info("Linking '{}' from '{}'", p::ToString(filePath), irModule.objectFile);
63-
p::RunProcess(command,
64-
SubprocessOptions::TerminateIfDestroyed | SubprocessOptions::CombinedOutErr);
73+
auto process = p::RunProcess(command,
74+
SubprocessOptions::TerminateIfDestroyed | SubprocessOptions::CombinedOutErr);
75+
i32 returnCode = 0;
76+
p::WaitProcess(process.TryGet(), &returnCode);
77+
if (returnCode != 0)
78+
{
79+
compiler.AddError("Linking failed");
80+
}
6581
}
6682
}
6783
}

Libs/Bindings/Native/Compiler/Include/Components/CNativeBinding.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace rift
1212
STRUCT(CNativeBinding, p::Struct)
1313

1414
PROP(binaries)
15-
p::TArray<p::Path> binaries;
15+
p::TArray<p::String> binaries;
16+
17+
PROP(autoGenerateDefinitions)
18+
bool autoGenerateDefinitions = false;
1619
};
1720
} // namespace rift

Libs/Bindings/Native/Compiler/Src/NativeBindingModule.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ namespace rift
103103
TArray<AST::Id> moduleIds;
104104
p::ecs::ListAll<AST::CModule, CNativeBinding>(ast, moduleIds);
105105

106+
// Only use automatic native bindings on modules marked as such
107+
moduleIds.RemoveIfSwap([ast](auto id) {
108+
return !ast.Get<CNativeBinding>(id).autoGenerateDefinitions;
109+
});
110+
106111
TArray<ParsedModule> parsedModules;
107112
parsedModules.Reserve(moduleIds.Size());
108113
for (i32 i = 0; i < moduleIds.Size(); ++i)

Libs/Runtimes/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/Lib/*

Libs/Runtimes/IO/__module__.rf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"CModule": {
3+
"target": "Static",
34
"dependencies": []
45
},
56
"CNativeBinding": {

0 commit comments

Comments
 (0)