Skip to content

Commit

Permalink
Synced our CppSharp fork with main (#8)
Browse files Browse the repository at this point in the history
* Incorrect build instructions on Windows (mono#1701)

* Fix incorrect build instructions for Windows

* Add link to Visual Studio installer

* Refine building instructions for VS2019+

(cherry picked from commit c4a24b0)

* Misc changes (mono#1710)

* Minor code refactoring for re-use.

* Add Class.FindVariable helper method.

* Upgrade to .NET 6.

(cherry picked from commit db7949b)

* Add C++ WebAssembly ABI (mono#1711)

* Minor code refactorings.

* Fix debug assert issue with vtable methods.

* Add support for WebAssembly C++ ABI to parser and AST converter.

(cherry picked from commit 9b06e7b)

* Add initial Emscripten generator. (mono#1712)

(cherry picked from commit 117567d)

* Code cleanups and improvements (mono#1713)

* Code cleanups.

* Run GetterSetterToPropertyPass for more generators.

* Fixed compile warning when compiling parser bindings.

* Cleanup driver code.

* Remove dead 32-bit code.

* Reduce verbosity when Options.Quiet is set.

* Remove test compile-time warnings.

* Move .NET tests to tests/dotnet.

* Remove unused AST viewer code and premake-qt submodule.

* Move tests2/ contents to tests/.

(cherry picked from commit 4417dd9)

* Publish package to nuget.org when a version tag is created

(cherry picked from commit 5715df5)

* Allow passing a `-target-framework` option to `build.sh` (mono#1718)

Closes mono#1717.

(cherry picked from commit 8cf6e3f)

* Remove unused LLVM build flags.

(cherry picked from commit 84b7276)

* Update LLVM workflow to latest Action images.

(cherry picked from commit 3d32fc7)

* Update LLVM Windows workflow to latest Action images.

(cherry picked from commit f9f9e9c)

* Update action steps to latest to fix Node.js deprecation warnings.

(cherry picked from commit 7d6decd)

* Update LLVM build script for Visual Studio 2022.

(cherry picked from commit b8a16a4)

* Workaround Directory.Build.props getting picked up when building LLVM.

This is to workaround MSBuild CMake step from LLVM build in Windows
picking this up.

(cherry picked from commit 25e2c7c)

* Added variation of mono#1736

* Workaround parsing GNU system headers with GCC 11.0 version. (mono#1737)

Closes mono#1703.

Upstream bugs:

  llvm/llvm-project#51607
  llvm/llvm-project#53152

(cherry picked from commit 0272532)

* CppSharp.AST.Declaration - fix return QualifiedOriginalName, QualifiedLogicalOriginalName (mono#1740)

Fixed properties getters of QualifiedOriginalName, QualifiedLogicalOriginalName and methods GetQualifiedName(),  GatherNamespaces.

(cherry picked from commit 9923d79)

* CSharpExpressionPrinter: Wrap expression in parenthesis (mono#1741)

Wrap default parameter expressions in parentheses to ensure whole expressions are casted instead of only the first operand.

(cherry picked from commit 169f868)

* Update README.md

Remove Gitter since the link is 404 and we have not been using it anyway.

(cherry picked from commit 160efb4)

* CSharpExpressionPrinter: Recurse into operands (mono#1745)

* CSharpExpressionPrinter: Recurse into operands

Recursively call `VisitExpression` on the LHS and RHS of a binary
operator expression. This fixes the generation for complex default
parameters involving things other than two enumeration members.

* CSharpSources: Use `const` when possible

Generate `const` instead of `static` members when possible.
This allows generated members to be used when compile-time constants are
required, such as default parameters.

(cherry picked from commit 1ce9cb7)

* CSharp: More default parameter fixes (mono#1747)

- Expression generation for `ConstructorReference` now also recursively
calls `VisitExpression` for the argument if only one argument is
detected. This allows correct overload generation for functions taking
a variable as the default parameter value.
- Default parameters of pointer-to-enumeration types are now correctly
generated similar to primitive types.

(cherry picked from commit add3aba)

* Add type parameter to non-type template parameter decls. (mono#1749)

(cherry picked from commit bb31bd6)

* Update parser example to latest API.

(cherry picked from commit 8c1fa18)

* Fixed debugging printing when type printing delegate is not initialized.

(cherry picked from commit 9f3ce76)

* Array marshalling (mono#1748)

* Generator: Customization for const char[]

Allow the user to choose whether `const char[]` should be marshalled as
`string` or a normal `char` array in C#.

A new option `MarshalConstCharArrayAsString` is added, and is `true`
by default.

This helps in situations where the original C++ API distinguishes
between C-strings and char arrays using the two different notations.

* CSharpMarshal: Fix unknown length array marshal

For unknown length arrays, also run a conversion loop if the primitive
type encountered needs conversion (e.g. `char` to `sbyte`).

* CSharpTypePrinter: Fix for boolean arrays

(cherry picked from commit 357efec)

* Add qualified template name to GetCXXRecordDeclFromBaseType. (mono#1751)

(cherry picked from commit 626a362)

* SymbolResolver: Use filename when path cannot be found (mono#1752)

Uses bare filename to pass to `dlopen` when the full path cannot
be detected. This helps on systems where library paths are not the
same as $PATH.

(cherry picked from commit 3978fb3)

* CSharpSources: Dereference pointer variables (mono#1753)

Dereference pointers when generating getters for pointer variables.
Otherwise, the managed instance would point to the pointer itself
rather than the object at the native instance's address.

(cherry picked from commit ce3d04a)

* Use TypePrinter.IntPtrType in CSharpSources.cs code from previous commit.

(cherry picked from commit 3c31179)

---------

Co-authored-by: aybe <[email protected]>
Co-authored-by: João Matos <[email protected]>
Co-authored-by: josetr <[email protected]>
Co-authored-by: stachu99 <[email protected]>
Co-authored-by: Trung Nguyen <[email protected]>
  • Loading branch information
6 people authored Sep 7, 2023
1 parent 2bf3d05 commit 0c3e1b4
Show file tree
Hide file tree
Showing 215 changed files with 2,284 additions and 3,652 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/llvm-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
fail-fast: false
matrix:
config:
- { os: windows-2019, platform: x86, vs: 2019 }
- { os: windows-2019, platform: x64, vs: 2019 }
- { os: windows-2022, platform: x86, vs: 2022 }
- { os: windows-2022, platform: x64, vs: 2022 }

runs-on: ${{ matrix.config.os }}

Expand All @@ -23,7 +23,7 @@ jobs:

steps:
- name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v3.x
uses: rlespinasse/github-slug-action@v4

- name: Check out
shell: cmd
Expand All @@ -35,7 +35,7 @@ jobs:
- name: Environment
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\%VS_VERSION%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" %PLATFORM%
call "C:\Program Files\Microsoft Visual Studio\%VS_VERSION%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" %PLATFORM%
:: Loop over all environment variables and make them global using set-env.
:: See: https://stackoverflow.com/a/39184941
setlocal
Expand All @@ -60,7 +60,7 @@ jobs:
working-directory: C:\CppSharp

- name: 'Upload Artifact'
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: llvm
path: C:\CppSharp\build\llvm\llvm-*-*.*
10 changes: 5 additions & 5 deletions .github/workflows/llvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
fail-fast: false
matrix:
config:
- { os: ubuntu-20.04, platform: x64, cxx: g++-9, cc: gcc-9 }
- { os: ubuntu-20.04, platform: x64, cxx: g++-10, cc: gcc-10 }
- { os: macos-10.15, platform: x64, cxx: clang++, cc: clang }
- { os: ubuntu-22.04, platform: x64, cxx: g++-11, cc: gcc-11 }
- { os: macos-11, platform: x64, cxx: clang++, cc: clang }
- { os: macos-12, platform: x64, cxx: clang++, cc: clang }

runs-on: ${{ matrix.config.os }}

Expand All @@ -22,7 +22,7 @@ jobs:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: lukka/get-cmake@latest

- name: Clone LLVM
Expand All @@ -38,7 +38,7 @@ jobs:
run: build/build.sh package_llvm -platform $PLATFORM

- name: 'Upload Artifact'
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: llvm
path: build/llvm/llvm-*-*.*
10 changes: 8 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ jobs:
runs-on: windows-2019
needs: build
if: |
(github.event_name == 'push' && (github.ref == 'refs/heads/main' || contains(github.ref, 'nuget'))) ||
(github.event.pull_request.head.repo.full_name != github.repository && contains(github.event.pull_request.head.ref, 'nuget'))
(github.event_name == 'push' && (github.ref == 'refs/heads/main' || contains(github.ref, 'nuget') || startsWith(github.ref, 'refs/tags/v'))) ||
(github.event.pull_request.head.repo.full_name != github.repository && (contains(github.event.pull_request.head.ref, 'nuget') || startsWith(github.event.pull_request.head.ref, 'refs/tags/v')))
env:
DOTNET_NOLOGO: true
Expand Down Expand Up @@ -111,3 +111,9 @@ jobs:
run: |
cd artifacts
dotnet nuget push "*.nupkg" --api-key ${{ secrets.GITHUB_TOKEN }} --source "https://nuget.pkg.github.com/mono/index.json" --skip-duplicate
- name: Publish package to nuget.org
if: startsWith(github.ref, 'refs/tags/v')
run: |
cd artifacts
dotnet nuget push "*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --source "https://api.nuget.org/v3/index.json" --skip-duplicate
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "build/modules/premake-qt"]
path = build/modules/premake-qt
url = https://github.com/dcourtois/premake-qt.git
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<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>
<RID Condition="$(IsWindows)">win</RID>
<RID Condition="$(IsWindows)">win</RID>
<RID Condition="$(IsLinux)">linux</RID>
<RID Condition="$(IsMacOSX)">osx</RID>
<RID>$(RID)-$(PlatformTarget)</RID>
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ Please see the following resources for more information:

Feel free to open up issues on Github for any problems you find.

You can also join us at our [#managed-interop](https://gitter.im/managed-interop) Gitter discussion channel.

## Support

For building wrappers and priority support please write to &#99;&#112;&#112;&#115;&#104;&#97;&#114;&#112;&#64;&#112;&#114;&#111;&#116;&#111;&#110;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;.
Expand Down
19 changes: 17 additions & 2 deletions build/Helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ newoption {
description = "Only generate configuration file",
}

newoption {
trigger = "target-framework",
description = ".NET target framework version",
}

rootdir = path.getabsolute("../")
srcdir = path.join(rootdir, "src");
incdir = path.join(rootdir, "include");
examplesdir = path.join(rootdir, "examples");
testsdir = path.join(rootdir, "tests");
testsdir = path.join(rootdir, "tests/dotnet");
builddir = path.join(rootdir, "build")
bindir = path.join(rootdir, "bin")
objsdir = path.join(builddir, "obj");
Expand All @@ -61,7 +66,17 @@ 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.isempty(s)
return s == nil or s == ''
end

local function target_framework()
local value = _OPTIONS["target-framework"]
return string.isempty(value) and "net6.0" or value
end

targetframework = target_framework()

function string.starts(str, start)
if str == nil then return end
Expand Down
12 changes: 9 additions & 3 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ vs=vs2019
configuration=Release
build_only=false
ci=false
target_framework=
verbosity=minimal
rootdir="$builddir/.."
bindir="$rootdir/bin"
Expand Down Expand Up @@ -46,18 +47,18 @@ build()

generate_config()
{
"$builddir/premake.sh" --file="$builddir/premake5.lua" $vs --os=$os --arch=$platform --configuration=$configuration --config_only
"$builddir/premake.sh" --file="$builddir/premake5.lua" $vs --os=$os --arch=$platform --configuration=$configuration --target-framework=$target_framework --config_only
}

generate()
{
download_llvm

if [ "$os" = "linux" ] || [ "$os" = "macosx" ]; then
"$builddir/premake.sh" --file="$builddir/premake5.lua" gmake2 --os=$os --arch=$platform --configuration=$configuration "$@"
"$builddir/premake.sh" --file="$builddir/premake5.lua" gmake2 --os=$os --arch=$platform --configuration=$configuration --target-framework=$target_framework "$@"
fi

"$builddir/premake.sh" --file="$builddir/premake5.lua" $vs --os=$os --arch=$platform --configuration=$configuration
"$builddir/premake.sh" --file="$builddir/premake5.lua" $vs --os=$os --arch=$platform --configuration=$configuration --target-framework=$target_framework
}

restore()
Expand Down Expand Up @@ -194,6 +195,11 @@ while [[ $# > 0 ]]; do
os=$2
shift
;;
-target-framework)
target_framework=$2
echo $target_framework
shift
;;
-ci)
ci=true
export CI=true
Expand Down
3 changes: 3 additions & 0 deletions build/llvm/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<!-- This empty file is to workaround MSBuild picking up the root Directy.Build.props when building LLVM. -->
</Project>
12 changes: 7 additions & 5 deletions build/llvm/LLVM.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ end

function get_vs_version()
local function map_msvc_to_vs_version(major, minor)
if major == "19" and minor >= "20" then return "vs2019"
if major == "19" and minor >= "30" then return "vs2022"
elseif major == "19" and minor >= "20" then return "vs2019"
elseif major == "19" and minor >= "10" then return "vs2017"
end
end

local function map_msbuild_to_vs_version(major, minor)
if major == "16" then return "vs2019"
if major == "17" then return "vs2022"
elseif major == "16" then return "vs2019"
elseif major == "15" then return "vs2017"
end
end
Expand Down Expand Up @@ -379,8 +381,6 @@ function cmake(gen, conf, builddir, options)
.. ' -DCLANG_TOOL_C_INDEX_TEST_BUILD=false'
.. ' -DCLANG_TOOL_DIAGTOOL_BUILD=false'
.. ' -DCLANG_TOOL_DRIVER_BUILD=false'
.. ' -DCLANG_TOOL_HANDLE_CXX_BUILD=false'
.. ' -DCLANG_TOOL_HANDLE_LLVM_BUILD=false'
.. ' -DCLANG_TOOL_LIBCLANG_BUILD=false'
.. ' -DCLANG_TOOL_SCAN_BUILD_BUILD=false'
.. ' -DCLANG_TOOL_SCAN_BUILD_PY_BUILD=false'
Expand All @@ -399,7 +399,9 @@ end

function get_cmake_generator()
local vsver = get_vs_version()
if vsver == "vs2019" then
if vsver == "vs2022" then
return "Visual Studio 17 2022", (target_architecture() == "x86") and "-A Win32" or nil
elseif vsver == "vs2019" then
return "Visual Studio 16 2019", (target_architecture() == "x86") and "-A Win32" or nil
elseif vsver == "vs2017" then
return "Visual Studio 15 2017" .. (target_architecture() == "x64" and " Win64" or ""), nil
Expand Down
1 change: 0 additions & 1 deletion build/modules/premake-qt
Submodule premake-qt deleted from 0ddc49
2 changes: 1 addition & 1 deletion build/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ workspace "CppSharp"
workspacefiles(path.join(builddir, "premake5.lua"))
workspacefiles(path.join(builddir, "*.sh"))
workspacefiles(path.join(rootdir, ".github/workflows/*.yml"))
workspacefiles(path.join(rootdir, "tests/Test*.props"))
workspacefiles(path.join(testsdir, "Test*.props"))

group "Libraries"
if EnableNativeProjects() then
Expand Down
27 changes: 24 additions & 3 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,46 @@ make sure those are installed on your system.

## Compiling on Windows/Visual Studio

The following steps should be called from the VS developer command prompt.

1. Generate the VS solution

```shell
cd <CppSharp>\build
./build.sh generate -configuration Release -platform x64
<sh> build.sh generate -configuration Release -platform x64
```

> :information_source: You can use the `-target-framework` option to target any valid .NET target framework.

2. Compile the VS projects

You can open `CppSharp.sln` and hit F5 or compile via the command line:

```
./build.sh -configuration Release -platform x64
<sh> build.sh -configuration Release -platform x64
```

Building in *Release* is recommended because else we will use the Clang parser
debug configuration, which will be too slow for practical use beyond debugging.

The solution generated will be for Visual Studio 2019.

If you have a more recent version of Visual Studio, you can either:
- install Visual Studio 2019
- install Visual Studio 2019 build tools, from [Visual Studio Installer](https://visualstudio.microsoft.com/downloads/)
- select the payload *MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.29-16.11)*

Please note that Windows isn't natively able to run sh scripts, to remediate this, you can either:
- use the one from Visual Studio if present
- e.g. `C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\usr\bin\sh.exe`
- use the one from [Git for Windows](https://gitforwindows.org/)
- e.g. `C:\Program Files\Git\bin\sh.exe`
When opening the solution for the first time on a more recent version than Visual Studio 2019, you will be prompted to retarget projects to one of the platform toolset available on your system.
## Compiling on macOS or Linux
The following steps should be called from the VS developer command prompt.
1. Generate the VS solution and makefiles
Expand All @@ -65,6 +84,8 @@ The following steps should be called from the VS developer command prompt.
./build.sh generate -configuration Release -platform x64
```
> :information_source: You can use the `-target-framework` option to target any valid .NET target framework.
2. Compile the csproj files and makefiles
```
Expand Down
1 change: 1 addition & 0 deletions docs/UsersManual.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ There is also experimental support for these JavaScript-related targets:
- N-API (Node.js)
- QuickJS
- TypeScript
- Emscripten

# 3. Native Targets

Expand Down
11 changes: 4 additions & 7 deletions examples/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static void Main(string[] args)
ParseSourceFile(file);
}

public static bool ParseSourceFile(string file)
private static bool ParseSourceFile(string file)
{
// Lets setup the options for parsing the file.
var parserOptions = new ParserOptions
{
LanguageVersion = LanguageVersion.CPP11,
LanguageVersion = LanguageVersion.CPP20_GNU,

// Verbose here will make sure the parser outputs some extra debugging
// information regarding include directories, which can be helpful when
Expand All @@ -37,13 +37,10 @@ public static bool ParseSourceFile(string file)
};

// This will setup the necessary system include paths and arguments for parsing.
// It will probe into the registry (on Windows) and filesystem to find the paths
// of the system toolchains and necessary include directories.
parserOptions.Setup();
parserOptions.Setup(Platform.Host);

// We create the Clang parser and parse the source code.
var parser = new ClangParser();
var parserResult = parser.ParseSourceFile(file, parserOptions);
var parserResult = ClangParser.ParseSourceFile(file, parserOptions);

// If there was some kind of error parsing, then lets print some diagnostics.
if (parserResult.Kind != ParserResultKind.Success)
Expand Down
3 changes: 2 additions & 1 deletion src/AST/ASTContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum CppAbi
Microsoft,
ARM,
iOS,
iOS64
iOS64,
WebAssembly
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/AST/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ public Method FindMethodByUSR(string usr)
.FirstOrDefault(m => m.USR == usr);
}

public Variable FindVariable(string name)
{
return Variables.FirstOrDefault(m => m.Name == name);
}

public override T Visit<T>(IDeclVisitor<T> visitor)
{
return visitor.VisitClassDecl(this);
Expand Down
9 changes: 1 addition & 8 deletions src/AST/ClassLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ public struct VTableComponent
public Declaration Declaration;

/// Method declaration (if Kind == FunctionPointer).
public Method Method
{
get
{
Debug.Assert(Kind == VTableComponentKind.FunctionPointer);
return Declaration as Method;
}
}
public Method Method => Declaration as Method;
}

/// <summary>
Expand Down
Loading

0 comments on commit 0c3e1b4

Please sign in to comment.