Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/BasicModelInterface.Tests/BasicModelInterface.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -21,6 +21,11 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnvironmentVariables>
<EnvironmentVariables>
<Variable name="DYLD_LIBRARY_PATH" value="/Users/baart_f/.local/lib:/opt/local/lib:/opt/local/lib/libgcc" />
</EnvironmentVariables>
</EnvironmentVariables>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
Expand All @@ -48,10 +54,11 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BasicModelInterface\BasicModelInterface.csproj">
<Project>{b973019e-5674-4e23-9b1e-c4ca5eac381b}</Project>
<Project>{B973019E-5674-4E23-9B1E-C4CA5EAC381B}</Project>
<Name>BasicModelInterface</Name>
</ProjectReference>
</ItemGroup>
Expand Down
23 changes: 18 additions & 5 deletions src/BasicModelInterface.Tests/BasicModelInterfaceLibraryTest.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
using System;
using NUnit.Framework;
using System.Runtime.InteropServices;

namespace BasicModelInterface.Tests
{
[TestFixture]
public class BasicModelInterfaceLibraryTest
{
private const string LibraryC = @"..\..\..\..\bin\Debug\model-c.dll";

private const string EngineName = @"modelc";
private string libraryPath;
private const string LibraryC = @"..\..\..\..\bin\Debug\model-c.dll";
private IBasicModelInterface library;

[SetUp]
public void SetUp()
{
library = new BasicModelInterfaceLibrary(LibraryC);
libraryPath = BasicModelInterfaceLibrary.GetLibraryPath(EngineName);
library = new BasicModelInterfaceLibrary(libraryPath);
}

[Test]
public void InitializeAndFinish()
{
const string configFilePath = "empty";

library.Initialize(configFilePath);
library.Finish();
}
Expand All @@ -45,7 +47,7 @@ public void GetVariableNames()
[Test]
public void Run()
{
BasicModelInterfaceLibrary.Run(LibraryC, "test.config");
BasicModelInterfaceLibrary.Run(libraryPath, "test.config");
}

[Test]
Expand Down Expand Up @@ -111,5 +113,16 @@ public void SetLogger()
library.Initialize(string.Empty);
library.Finish();
}

[Test]
public void InitializeDirect()
{
initialize ("");
}

[DllImport ("modelc", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int initialize (string configPath);


}
}
4 changes: 4 additions & 0 deletions src/BasicModelInterface.Tests/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<dllmap dll="modelc" target="libmodelc.0.dylib" />
</configuration>
6 changes: 3 additions & 3 deletions src/BasicModelInterface.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.3" targetFramework="net45" />
</packages>
5 changes: 4 additions & 1 deletion src/BasicModelInterface/BasicModelInterface.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -38,6 +38,9 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="libmodelc-csharp.dll">
<HintPath>..\..\..\..\.local\lib\libmodelc-csharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BasicModelInterfaceLibrary.cs" />
Expand Down
63 changes: 63 additions & 0 deletions src/BasicModelInterface/BasicModelInterfaceLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -32,6 +34,67 @@ public BasicModelInterfaceLibrary(string libraryPath, CallingConvention callingC
lib.ReturnTypes["finalize"] = typeof(int);
}

public static string LibraryName(string engine)
{
string extension = ".dll";
string prefix = "";
var p = Environment.OSVersion.Platform;
if (p == PlatformID.Unix || p == PlatformID.MacOSX)
{
prefix = "lib";
extension = ".so";

// HACK: Mono implements Platform incorrectly
if (IsRunningOnMac ()) {
extension = ".dylib";
extension = "";
}
}
else {
prefix = "";
extension = ".dll";
};
return prefix + engine + extension;
}

//From Managed.Windows.Forms/XplatUI
[DllImport ("libc")]
static extern int uname (IntPtr buf);

static bool IsRunningOnMac ()
{
IntPtr buf = IntPtr.Zero;
try {
buf = Marshal.AllocHGlobal (8192);
// This is a hacktastic way of getting sysname from uname ()
if (uname (buf) == 0) {
string os = Marshal.PtrToStringAnsi (buf);
if (os == "Darwin")
return true;
}
} catch {
} finally {
if (buf != IntPtr.Zero)
Marshal.FreeHGlobal (buf);
}
return false;
}

public static string GetLibraryPath(string engine)
{
string[] knownPaths = {
"~/.local/lib",
"/usr/local/lib",
"/usr/lib"
};
// TODO loop over paths to find the library.

string expandedPath = knownPaths[0].Replace("~", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
var fullPath = Path.GetFullPath (Path.Combine (expandedPath, LibraryName (engine)));
Debug.Assert (new FileInfo (fullPath).Exists);
return fullPath;
}

public DateTime StartTime
{
get
Expand Down
2 changes: 2 additions & 0 deletions src/BasicModelInterface/Interop/DynamicDllImportMetaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, Dy

private Type GetMethodReturnType(InvokeMemberBinder binder)
{
/*
var types = binder.GetType().GetField("m_typeArguments", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(binder) as IList<Type>;

if ((types != null) && (types.Count > 0))
{
return types[0];
}
*/

Type type;
lib.ReturnTypes.TryGetValue(binder.Name, out type);
Expand Down
17 changes: 9 additions & 8 deletions src/BasicModelInterfaceRunner/BasicModelInterfaceRunner.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -32,37 +32,38 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DocoptNet">
<HintPath>..\packages\docopt.net.0.6.1.5\lib\net40\DocoptNet.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="DocoptNet">
<HintPath>..\..\packages\docopt.net.0.6.1.5\lib\net40\DocoptNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="Main.usage.txt" />
<None Include="T4DocoptNet.tt" />
<None Include="T4DocoptNet.tt.hooks.t4" />
<None Include="T4DocoptNet.tt.settings.xml" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BasicModelInterface\BasicModelInterface.csproj">
<Project>{b973019e-5674-4e23-9b1e-c4ca5eac381b}</Project>
<Project>{B973019E-5674-4E23-9B1E-C4CA5EAC381B}</Project>
<Name>BasicModelInterface</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<Content Include="Main.usage.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
78 changes: 39 additions & 39 deletions src/BasicModelInterfaceRunner/Main.usage.txt
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
Example usage for T4 Docopt.NET

Usage:
prog command ARG <myarg> [OPTIONALARG] [-o -s=<arg> --long=ARG --switch]
prog files FILE...

Options:
-o Short switch.
-s=<arg> Short option with arg.
--long=ARG Long option with arg.
--swith Long switch.

Explanation:
This is an example usage file that needs to be customized.
Every time you change this file, run the Custom Tool command
on T4DocoptNet.tt to re-generate the MainArgs class
(defined in T4DocoptNet.cs).
You can then use the MainArgs classed as follows:

class Program
{

static void DoStuff(string arg, bool flagO, string longValue)
{
// ...
}

static void Main(string[] argv)
{
// Automatically exit(1) if invalid arguments
var args = new MainArgs(argv, exit: true);
if (args.CmdCommand)
{
Console.WriteLine("First command");
DoStuff(args.ArgArg, args.OptO, args.OptLong);
}
}
}

Example usage for T4 Docopt.NET
Usage:
prog command ARG <myarg> [OPTIONALARG] [-o -s=<arg> --long=ARG --switch]
prog files FILE...
Options:
-o Short switch.
-s=<arg> Short option with arg.
--long=ARG Long option with arg.
--swith Long switch.
Explanation:
This is an example usage file that needs to be customized.
Every time you change this file, run the Custom Tool command
on T4DocoptNet.tt to re-generate the MainArgs class
(defined in T4DocoptNet.cs).
You can then use the MainArgs classed as follows:
class Program
{
static void DoStuff(string arg, bool flagO, string longValue)
{
// ...
}
static void Main(string[] argv)
{
// Automatically exit(1) if invalid arguments
var args = new MainArgs(argv, exit: true);
if (args.CmdCommand)
{
Console.WriteLine("First command");
DoStuff(args.ArgArg, args.OptO, args.OptLong);
}
}
}
Loading