Skip to content

Commit

Permalink
add test for static library, make it pass on unix (dotnet#4985)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed Feb 24, 2018
1 parent 7635542 commit 35e84f6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 4 deletions.
8 changes: 4 additions & 4 deletions tests/src/Simple/SharedLibrary/SharedLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<Target Name="NativeRunnerCompile" AfterTargets="LinkNative">
<PropertyGroup>
<NativeRunnerObject>$(NativeOutputPath)SharedLibrary</NativeRunnerObject>
<NativeRunnerBinary>$(NativeOutputPath)SharedLibrary</NativeRunnerBinary>
</PropertyGroup>

<ItemGroup>
Expand All @@ -20,9 +20,9 @@

<ItemGroup>
<NativeRunnerCompilerArg Include="@(CppCompile)" />
<NativeRunnerCompilerArg Include="-o $(NativeRunnerObject)" Condition="'$(OS)' != 'Windows_NT'" />
<NativeRunnerCompilerArg Include="/Fo$(NativeRunnerObject)" Condition="'$(OS)' == 'Windows_NT'" />
<NativeRunnerCompilerArg Include="/Fe$(NativeRunnerObject)" Condition="'$(OS)' == 'Windows_NT'" />
<NativeRunnerCompilerArg Include="-o $(NativeRunnerBinary)" Condition="'$(OS)' != 'Windows_NT'" />
<NativeRunnerCompilerArg Include="/Fo$(NativeRunnerBinary)" Condition="'$(OS)' == 'Windows_NT'" />
<NativeRunnerCompilerArg Include="/Fe$(NativeRunnerBinary)" Condition="'$(OS)' == 'Windows_NT'" />
</ItemGroup>

<Exec Command="$(CppCompiler) @(NativeRunnerCompilerArg, ' ')" Condition="'$(OS)' != 'Windows_NT'" />
Expand Down
10 changes: 10 additions & 0 deletions tests/src/Simple/StaticLibrary/NativeCallable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace System.Runtime.InteropServices
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class NativeCallableAttribute : Attribute
{
public string EntryPoint;
public CallingConvention CallingConvention;
public NativeCallableAttribute() { }
}
}
19 changes: 19 additions & 0 deletions tests/src/Simple/StaticLibrary/StaticLibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "stdio.h"

extern "C" int Add(int a, int b);
extern "C" int Subtract(int a, int b);
extern "C" bool Not(bool b);

int main()
{
if (Add(2, 3) != 5)
return 1;

if (Subtract(3, 1) != 2)
return 1;

if (!Not(false))
return 1;

return 100;
}
26 changes: 26 additions & 0 deletions tests/src/Simple/StaticLibrary/StaticLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Runtime.InteropServices;

namespace StaticLibrary
{
public class ClassLibrary
{
[NativeCallable(EntryPoint = "Add", CallingConvention = CallingConvention.StdCall)]
public static int Add(int a, int b)
{
return a + b;
}

[NativeCallable(EntryPoint = "Subtract", CallingConvention = CallingConvention.StdCall)]
public static int Subtract(int a, int b)
{
return a - b;
}

[NativeCallable(EntryPoint = "Not", CallingConvention = CallingConvention.StdCall)]
public static bool Not(bool b)
{
return !b;
}
}
}
31 changes: 31 additions & 0 deletions tests/src/Simple/StaticLibrary/StaticLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<OutputType>Library</OutputType>
<NativeLib>Static</NativeLib>
</PropertyGroup>

<ItemGroup>
<Compile Include="*.cs" />
</ItemGroup>

<Target Name="NativeRunnerCompile" AfterTargets="LinkNative">
<PropertyGroup>
<NativeRunnerBinary>$(NativeOutputPath)StaticLibrary</NativeRunnerBinary>
</PropertyGroup>

<ItemGroup>
<CppCompile Include="StaticLibrary.cpp" />
</ItemGroup>

<ItemGroup>
<NativeRunnerCompilerArg Include="@(CppCompile)" />
<NativeRunnerCompilerArg Include="-o $(NativeRunnerBinary)" Condition="'$(OS)' != 'Windows_NT'" />
</ItemGroup>

<Exec Command="$(CppCompiler) @(NativeRunnerCompilerArg, ' ') $(NativeBinary) @(NativeLibrary, ' ') -g -Wl,-rpath,'$ORIGIN' -pthread -lstdc++ -ldl -lm -licucore" Condition="'$(OS)' != 'Windows_NT'" />
</Target>

<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), SimpleTest.targets))\SimpleTest.targets" />

</Project>
9 changes: 9 additions & 0 deletions tests/src/Simple/StaticLibrary/StaticLibrary.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
$1/$2
if [ $? == 100 ]; then
echo pass
exit 0
else
echo fail
exit 1
fi
1 change: 1 addition & 0 deletions tests/src/Simple/StaticLibrary/no_cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Skip this test for cpp codegen mode

0 comments on commit 35e84f6

Please sign in to comment.