Skip to content

Commit

Permalink
Only use premake for native projects and improve c# projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
josetr committed Dec 1, 2020
1 parent 0b270df commit 1926e69
Show file tree
Hide file tree
Showing 55 changed files with 369 additions and 259 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ src/generator/generator
*.filters
*.sln
*.metagen
*.csproj
*.ilk
*.manifest
*.tmp
Expand All @@ -43,6 +42,7 @@ src/generator/generator
/build/vs20*
/build/gmake
/build/headers
/build/config.props
/build/premake/premake5*
/build/gen
/deps/llvm
Expand Down
30 changes: 28 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
<Project>
<Import Project="build/config.props" />

<PropertyGroup>
<RootDir>$(MSBuildThisFileDirectory)</RootDir>
<Platforms>x86;x64</Platforms>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<BaseIntermediateOutputPath>$(RootDir)build\obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
<TargetDir Condition="$(Configuration) != ''">$(RootDir)bin\$(Configuration)_$(Platform)</TargetDir>
<BuildDir>$(RootDir)build\</BuildDir>
<ObjDir>$(BuildDir)obj\</ObjDir>
<GenDir>$(BuildDir)gen\</GenDir>
<SrcDir>$(RootDir)src\</SrcDir>
<BaseIntermediateOutputPath>$(ObjDir)$(MSBuildProjectName)\</BaseIntermediateOutputPath>
<BaseOutputPath>$(RootDir)bin\</BaseOutputPath>
<OutputPath>$(BaseOutputPath)$(Configuration)_$(PlatformTarget)\</OutputPath>
<ActionDir>$(BuildDir)$(PremakeAction)\</ActionDir>
<NativeProjectsDir>$(ActionDir)projects\</NativeProjectsDir>
<TargetDir>$(OutputPath)</TargetDir>
<LangVersion>7.3</LangVersion>
<WarningLevel>4</WarningLevel>
<DotNetCmd>dotnet</DotNetCmd>
<GeneratorFileExtension>dll</GeneratorFileExtension>
<DotNetCmd Condition="'$(PlatformTarget)' == 'x86' AND Exists('$(MSBuildProgramFiles32)\dotnet\dotnet.exe')">"$(MSBuildProgramFiles32)\dotnet\dotnet.exe"</DotNetCmd>
<DotNetCmd Condition="'$(PlatformTarget)' == 'x64' AND Exists('$(ProgramW6432)\dotnet\dotnet.exe')">"$(ProgramW6432)\dotnet\dotnet.exe"</DotNetCmd>
</PropertyGroup>

<PropertyGroup Condition="'$(CPPSHARP_RELEASE)' != 'true'">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>

<PropertyGroup Condition="'$(DotNetCmd)' == 'dotnet' AND $(IsWindows)">
<GeneratorFileExtension>exe</GeneratorFileExtension>
<DotNetCmd></DotNetCmd>
</PropertyGroup>
</Project>
38 changes: 38 additions & 0 deletions build/Helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ msvc_cpp_defines = { }
default_gcc_version = "9.0.0"
generate_build_config = true
premake.path = premake.path .. ";" .. path.join(builddir, "modules")
targetframework = "netcoreapp3.1"

function string.starts(str, start)
if str == nil then return end
Expand All @@ -73,6 +74,10 @@ end
function SetupNativeProject()
location (path.join(actionbuilddir, "projects"))
files { "*.lua" }

if os.getenv("CPPSHARP_RELEASE") == "true" then
symbols "off"
end

filter { "configurations:Debug" }
defines { "DEBUG" }
Expand Down Expand Up @@ -118,11 +123,17 @@ function SetupNativeProject()
end

function SetupManagedProject()
kind "SharedLib"
language "C#"
location "."
filter {}
end

function SetupExternalManagedProject(name)
externalproject (name)
SetupManagedProject()
end

function IncludeDir(dir)
local deps = os.matchdirs(dir .. "/*")

Expand Down Expand Up @@ -230,3 +241,30 @@ function AddPlatformSpecificFiles(folder, filename)
print "Unknown architecture"
end
end

function WriteConfigForMSBuild()
local file = io.open("config.props", "w+")
function writeProperty(name, value)
file:write(" <" .. name .. ">" .. (value ~= nil and value or "") .. "</" .. name .. ">\n")
end
function writeBooleanProperty(name, value)
writeProperty(name, value and "true" or "false")
end

file:write("<!-- GENERATED FILE -->\n")
file:write("<Project>\n")
file:write(" <PropertyGroup>\n")
writeProperty("PlatformTarget", target_architecture())
writeProperty("TargetFramework", targetframework)
writeProperty("Configuration", _OPTIONS["configuration"])
writeBooleanProperty("IsWindows", os.istarget("windows"))
writeBooleanProperty("IsLinux", os.istarget("linux"))
writeBooleanProperty("IsMacOSX", os.istarget("macosx"))
writeBooleanProperty("CI", os.getenv("CI") == "true")
writeBooleanProperty("GenerateBuildConfig", generate_build_config == true)
writeBooleanProperty("UseCXX11ABI", UseCxx11ABI())
writeProperty("PremakeAction", _ACTION)
file:write(" </PropertyGroup>\n")
file:write("</Project>")
file:close()
end
83 changes: 13 additions & 70 deletions build/Tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,15 @@ function SetupManagedTestProject()
files { "*.lua" }
end

function SetupTestGeneratorProject(name, depends)
if not EnabledManagedProjects() then
return
end
project(name .. ".Gen")
SetupManagedTestProject()
kind "ConsoleApp"
enabledefaultnoneitems "false"

files { name .. ".cs" }
dependson { name .. ".Native" }
links { "CppSharp.Generator.Tests" }

if depends ~= nil then
links { depends .. ".Gen" }
end

local command = os.ishost("windows") and "type nul >" or "touch"
postbuildcommands { command .. " " .. SafePath(path.join(objsdir, name .. ".Native", "timestamp.cs")) }
postbuildcommands { command .. " " .. SafePath(path.join(objsdir, name .. ".Native", "timestamp.cpp")) }
function SetupExternalManagedTestProject(name)
externalproject (name)
SetupManagedTestProject()
end

function SetupTestGeneratorBuildEvent(name)
local cmd = os.ishost("windows") and "" or "dotnet "
local ext = os.ishost("windows") and "exe" or "dll"
prebuildcommands { cmd .. SafePath(path.join("%{cfg.buildtarget.directory}", name .. ".Gen." .. ext)) }
function SetupTestGeneratorProject(name, depends)
if EnabledManagedProjects() then
SetupExternalManagedTestProject(name .. ".Gen")
end
end

function SetupTestNativeProject(name, depends)
Expand All @@ -86,10 +69,6 @@ function SetupTestNativeProject(name, depends)
if depends ~= nil then
links { depends .. ".Native" }
end

local command = os.ishost("windows") and "type nul >" or "touch"
postbuildcommands { command .. " " .. SafePath(path.join(objsdir, name .. ".Native", "timestamp.cs")) }
postbuildcommands { command .. " " .. SafePath(path.join(objsdir, name .. ".Native", "timestamp.cpp")) }
end

function SetupTestProjectsCSharp(name, depends, extraFiles, suffix)
Expand All @@ -103,35 +82,9 @@ function SetupTestProjectsCSharp(name, depends, extraFiles, suffix)
nm = name
str = "Std"
end
project(name .. ".CSharp")
SetupManagedTestProject()

dependson { name .. ".Gen", name .. ".Native", "CppSharp.Generator" }
SetupTestGeneratorBuildEvent(name)
enabledefaultnoneitems "false"

files
{
path.join(gendir, name, nm .. ".cs"),
path.join(gendir, name, str .. ".cs"),
path.join(objsdir, name .. ".Native", "timestamp.cs")
}

links { "CppSharp.Runtime" }

if depends ~= nil then
links { depends .. ".CSharp" }
end

project(name .. ".Tests.CSharp")
SetupManagedTestProject()

enabledefaultnoneitems "false"
files { name .. ".Tests.cs" }

links { name .. ".CSharp", "CppSharp.Generator.Tests", "CppSharp.Runtime" }
nuget { "Microsoft.NET.Test.Sdk:16.8.0" }
dependson { name .. ".Native" }

SetupExternalManagedTestProject(name .. ".CSharp")
SetupExternalManagedTestProject(name .. ".Tests.CSharp")
end

function SetupTestProjectsCLI(name, extraFiles, suffix)
Expand All @@ -142,14 +95,11 @@ function SetupTestProjectsCLI(name, extraFiles, suffix)
project(name .. ".CLI")
SetupNativeProject()

enabledefaultcompileitems "false"
enabledefaultnoneitems "false"
kind "SharedLib"
language "C++"
clr "NetCore"

dependson { name .. ".Gen", name .. ".Native", "CppSharp.Generator" }
SetupTestGeneratorBuildEvent(name)
dependson { name .. ".Gen" }

if (suffix ~= nil) then
nm = name .. suffix
Expand All @@ -174,16 +124,9 @@ function SetupTestProjectsCLI(name, extraFiles, suffix)

includedirs { path.join(testsdir, name), incdir }
links { name .. ".Native" }
files { path.join(objsdir, name .. ".Native", "timestamp.cpp") }

project(name .. ".Tests.CLI")
SetupManagedTestProject()
enabledefaultnoneitems "false"
files { name .. ".Tests.cs" }
files { path.join(objsdir, name .. ".Native") }

links { name .. ".CLI", "CppSharp.Generator.Tests" }
dependson { name .. ".Native" }
nuget { "Microsoft.NET.Test.Sdk:16.8.0" }
SetupExternalManagedTestProject(name .. ".Tests.CLI")
end

function IncludeExamples()
Expand Down
2 changes: 1 addition & 1 deletion build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ find_msbuild()
if [ -x "$(command -v MSBuild.exe)" ]; then
msbuild="MSBuild.exe"
else
msbuild="msbuild"
msbuild="dotnet msbuild"
fi
}

Expand Down
38 changes: 5 additions & 33 deletions build/premake/premake.extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,16 @@ premake.api.register {
kind = "list:string",
}

premake.api.register {
name = "removecompilefiles",
scope = "project",
kind = "list:string",
}

premake.api.register {
name = "enabledefaultnoneitems",
scope = "project",
kind = "boolean",
}

premake.override(premake.vstudio.dotnetbase.netcore, "enableDefaultCompileItems", function(base, cfg)
base(cfg);

if cfg.enabledefaultnoneitems ~= nil then
premake.w('<EnableDefaultNoneItems>%s</EnableDefaultNoneItems>', iif(cfg.enabledefaultnoneitems, "true", "false"))
end
end)

premake.override(premake.vstudio.dotnetbase, "files", function(base, prj)
base(prj);

if prj.removecompilefiles ~= nil then
for _, file in ipairs(prj.removecompilefiles) do
premake.w("<Compile Remove=\"%s\" />", file)
end
end
end)

-- https://github.com/premake/premake-core/issues/1061#issuecomment-441417853
premake.override(premake.vstudio.sln2005, "projects", function(base, wks)
if wks.workspacefiles and #wks.workspacefiles > 0 then
premake.push('Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{' .. os.uuid("Solution Items:"..wks.name) .. '}"')
premake.push("ProjectSection(SolutionItems) = preProject")
for _, file in ipairs(wks.workspacefiles) do
file = path.rebase(file, ".", wks.location)
premake.w(file.." = "..file)
for _, pattern in ipairs(wks.workspacefiles) do
local matches = os.matchfiles(pattern)
for _, file in ipairs(matches) do
premake.w(file.." = "..file)
end
end
premake.pop("EndProjectSection")
premake.pop("EndProject")
Expand Down
11 changes: 0 additions & 11 deletions build/premake/premake.fixes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,3 @@ premake.override(premake.vstudio.vc2010, "targetFramework", function(oldfn, prj)
oldfn(prj)
end
end)

-- https://github.com/premake/premake-core/issues/1549
premake.override(premake.vstudio.cs2005.elements, "projectProperties", function(base, cfg)
local calls = base(cfg);
table.insert(calls, function(cfg)
if cfg.clr == "Unsafe" then
premake.w('<AllowUnsafeBlocks>true</AllowUnsafeBlocks>')
end
end)
return calls;
end)
8 changes: 6 additions & 2 deletions build/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ include "Helpers.lua"
include "LLVM.lua"

workspace "CppSharp"

configurations { _OPTIONS["configuration"] }
platforms { target_architecture() }
dotnetframework "netcoreapp3.1"
dotnetframework (targetframework)
enabledefaultcompileitems "true"
characterset "Unicode"
symbols "On"
Expand All @@ -35,6 +34,11 @@ workspace "CppSharp"
end
end

workspacefiles(path.join(builddir, "premake5.lua"))
workspacefiles(path.join(builddir, "*.sh"))
workspacefiles(path.join(rootdir, "tests/Test*.props"))
WriteConfigForMSBuild()

group "Libraries"
if EnableNativeProjects() then
include (srcdir .. "/CppParser")
Expand Down
6 changes: 6 additions & 0 deletions src/AST/CppSharp.AST.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
</Project>
7 changes: 1 addition & 6 deletions src/AST/premake5.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
project "CppSharp.AST"

kind "SharedLib"
language "C#"

SetupManagedProject()
SetupExternalManagedProject("CppSharp.AST")
9 changes: 9 additions & 0 deletions src/CLI/CppSharp.CLI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Generator\CppSharp.Generator.csproj" />
</ItemGroup>
</Project>
9 changes: 1 addition & 8 deletions src/CLI/premake5.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
project "CppSharp.CLI"

SetupManagedProject()

kind "ConsoleApp"
language "C#"

links { "CppSharp.Generator" }
SetupExternalManagedProject("CppSharp.CLI")
11 changes: 11 additions & 0 deletions src/Core/CppSharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="2.3.2262-g94fae01e" PrivateAssets="All" />
</ItemGroup>
</Project>
Loading

0 comments on commit 1926e69

Please sign in to comment.