diff options
Diffstat (limited to 'Tizen.Runtime')
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime.Coreclr.csproj (renamed from Tizen.Runtime/Tizen.Runtime.csproj) | 10 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyLoader.cs (renamed from Tizen.Runtime/Tizen.Runtime/AssemblyLoader.cs) | 2 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyManager.cs (renamed from Tizen.Runtime/Tizen.Runtime/AssemblyManager.cs) | 138 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime.Mono.csproj | 75 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime.Mono/AssemblyManager.cs | 153 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime/Ini.cs | 139 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime/Interop.cs | 28 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime/Log.cs | 53 | ||||
-rw-r--r-- | Tizen.Runtime/Tizen.Runtime/test.cs | 21 |
9 files changed, 300 insertions, 319 deletions
diff --git a/Tizen.Runtime/Tizen.Runtime.csproj b/Tizen.Runtime/Tizen.Runtime.Coreclr.csproj index a0fce08..32864ed 100644 --- a/Tizen.Runtime/Tizen.Runtime.csproj +++ b/Tizen.Runtime/Tizen.Runtime.Coreclr.csproj @@ -4,8 +4,8 @@ <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <OutputType>exe</OutputType> - <AssemblyName>Tizen.Runtime</AssemblyName> + <OutputType>library</OutputType> + <AssemblyName>Tizen.Runtime.Coreclr</AssemblyName> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)' == 'Debug'"> @@ -34,11 +34,9 @@ </PropertyGroup> <ItemGroup> - <Compile Include="Tizen.Runtime/AssemblyManager.cs" /> - <Compile Include="Tizen.Runtime/AssemblyLoader.cs" /> + <Compile Include="Tizen.Runtime.Coreclr/AssemblyManager.cs" /> + <Compile Include="Tizen.Runtime.Coreclr/AssemblyLoader.cs" /> <Compile Include="Tizen.Runtime/Log.cs" /> - <Compile Include="Tizen.Runtime/Interop.cs" /> - <Compile Include="Tizen.Runtime/Ini.cs" /> <Compile Include="Tizen.Runtime/DefaultConfigAttribute.cs" /> </ItemGroup> diff --git a/Tizen.Runtime/Tizen.Runtime/AssemblyLoader.cs b/Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyLoader.cs index 4b4a30b..2e21c7b 100644 --- a/Tizen.Runtime/Tizen.Runtime/AssemblyLoader.cs +++ b/Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyLoader.cs @@ -4,7 +4,7 @@ using System.Reflection; using System.Runtime.Loader; using System.Collections.Generic; -namespace Tizen.Runtime +namespace Tizen.Runtime.Coreclr { public class AssemblyLoader : AssemblyLoadContext { diff --git a/Tizen.Runtime/Tizen.Runtime/AssemblyManager.cs b/Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyManager.cs index 6a13648..ec342d5 100644 --- a/Tizen.Runtime/Tizen.Runtime/AssemblyManager.cs +++ b/Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyManager.cs @@ -5,28 +5,43 @@ using System.Runtime.Loader; using System.Linq; using System.Runtime.InteropServices; -namespace Tizen.Runtime +namespace Tizen.Runtime.Coreclr { public static class AssemblyManager { public static bool Launch( + [In] string rootPath, [In] string path, [In] int argc, - [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] + [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)] [In] string[] argv) { - Console.WriteLine($"path : {path}"); - Console.WriteLine($"argc : {argc}"); - for (int i=0; i<argc; i++) + ALog.Debug($"Application Launch path : {path}"); + try { - Console.WriteLine($"argv[{i}] : " + argv[i]); + DirectoryInfo bindir = new DirectoryInfo(Path.Combine(rootPath, "bin")); + DirectoryInfo libdir = new DirectoryInfo(Path.Combine(rootPath, "lib")); + if (Directory.Exists(bindir.FullName)) + { + CurrentAssemblyLoaderContext.AddSearchableDirectory(bindir.FullName); + } + if (Directory.Exists(libdir.FullName)) + { + CurrentAssemblyLoaderContext.AddSearchableDirectory(libdir.FullName); + } + Execute(path, argv); + } + catch(Exception e) + { + ALog.Debug("Exception in Launch()"); + PrintException(e); + return false; } - Console.WriteLine(); return true; } - public static int Prepared(IntPtr data) + public static void Prepared() { try { @@ -44,74 +59,13 @@ namespace Tizen.Runtime if (!Initialize(preloadPath)) { - return 1; + ALog.Debug($"Failed to Initialized with {preloadPath}"); } } catch(Exception e) { ALog.Debug("Exception at Preparing"); PrintException(e); - return 1; - } - return 0; - } - - public static int Requested(IntPtr data) - { - return 0; - } - - public static int Executed(string path, string app_root, int argc, string[] argv, IntPtr data) - { - try - { - DirectoryInfo libdir = new DirectoryInfo(Path.Combine(app_root, "lib")); - if (Directory.Exists(libdir.FullName)) - { - CurrentAssemblyLoaderContext.AddSearchableDirectory(libdir.FullName); - } - Execute(path, argv); - } - catch(Exception e) - { - ALog.Debug("Exception at Executing"); - PrintException(e); - return 1; - } - - try - { - Finish(); - } - catch(Exception e) - { - ALog.Debug("Exception at Finishing"); - PrintException(e); - return 1; - } - return 0; - } - - static Interop.PreparedCallback preparedCallback; - static Interop.RequestedCallback requestedCallback; - static Interop.ExecutedCallback executedCallback; - - public static void Main(string[] args) - { - try - { - /* - preparedCallback = Prepared; - requestedCallback = Requested; - executedCallback = Executed; - Interop.register_launching_callback(preparedCallback, requestedCallback, executedCallback, IntPtr.Zero); - Interop.wait_for_launching(args.Length, args); - */ - Console.WriteLine("HelloWorld!!"); - } - catch(Exception e) - { - PrintException(e); } } @@ -146,35 +100,6 @@ namespace Tizen.Runtime } } } - - /* - string LD_LIBRARY_PATH = Environment.GetEnvironmentVariable("LD_LIBRARY_PATH"); - if (!string.IsNullOrEmpty(LD_LIBRARY_PATH)) - { - string[] dirs = LD_LIBRARY_PATH.Split(new char[]{':'}, StringSplitOptions.None); - foreach (string dir in dirs) - { - DirectoryInfo d = new DirectoryInfo(dir); - if (Directory.Exists(d.FullName)) - { - CurrentAssemblyLoaderContext.AddSearchableDirectory(d.FullName); - } - } - } - - DirectoryInfo libdir = new DirectoryInfo("/lib"); - if (Directory.Exists(libdir.FullName)) - { - CurrentAssemblyLoaderContext.AddSearchableDirectory(libdir.FullName); - } - - DirectoryInfo usrlibdir = new DirectoryInfo("/usr/lib"); - if (Directory.Exists(usrlibdir.FullName)) - { - CurrentAssemblyLoaderContext.AddSearchableDirectory(usrlibdir.FullName); - } - */ - } catch (Exception e) { @@ -205,21 +130,6 @@ namespace Tizen.Runtime } } - public static void Finish() - { - ALog.Debug("-------- Dll Search Dir"); - foreach (string dir in CurrentAssemblyLoaderContext.DllDirectories) - { - ALog.Debug(dir); - } - - ALog.Debug("-------- Native Search Dir"); - foreach (string dir in CurrentAssemblyLoaderContext.NativeDirectories) - { - ALog.Debug(dir); - } - } - public static AssemblyLoader CurrentAssemblyLoaderContext { get; diff --git a/Tizen.Runtime/Tizen.Runtime.Mono.csproj b/Tizen.Runtime/Tizen.Runtime.Mono.csproj new file mode 100644 index 0000000..e570697 --- /dev/null +++ b/Tizen.Runtime/Tizen.Runtime.Mono.csproj @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="14.0" InitialTargets="CheckConfig" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <OutputType>library</OutputType> + <AssemblyName>Tizen.Runtime.Mono</AssemblyName> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + </PropertyGroup> + + <PropertyGroup Condition=" '$(Configuration)' == 'Debug'"> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <OutputPath>bin/</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + </PropertyGroup> + + <PropertyGroup Condition=" '$(Configuration)' == 'Release'"> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin/</OutputPath> + <DefineConstants>TRACE</DefineConstants> + </PropertyGroup> + +<!-- Roslyn Not Support Assembly Signing yet. + <PropertyGroup> + <SignAssembly>true</SignAssembly> + <AssemblyOriginatorKeyFile>Tizen.Runtime.snk</AssemblyOriginatorKeyFile> + </PropertyGroup> +--> + + <PropertyGroup> + <DefineConstants Condition=" '$(CLOG)' != '' ">$(DefineConstants);CLOG</DefineConstants> + </PropertyGroup> + + <ItemGroup> + <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.Net.Http" /> + <Reference Include="System.Xml" /> + </ItemGroup> + + <ItemGroup> + <Compile Include="Tizen.Runtime.Mono/AssemblyManager.cs" /> + <Compile Include="Tizen.Runtime/Log.cs" /> + <Compile Include="Tizen.Runtime/DefaultConfigAttribute.cs" /> + </ItemGroup> + + <Target Name="CheckConfig"> + <Message Text="MSBuildProjectDirectory = $(MSBuildProjectDirectory)"/> + </Target> + + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + + <Target Name="BeforeCompile"> + <ItemGroup> + <AssemblyAttributes Include="DefaultConfigAttribute" Condition=" $(PreloadPath) != '' "> + <_Parameter1>PreloadPath=$(PreloadPath)</_Parameter1> + </AssemblyAttributes> + </ItemGroup> + + <WriteCodeFragment AssemblyAttributes="@(AssemblyAttributes)" + Language="C#" + OutputDirectory="$(IntermediateOutputPath)" + OutputFile="Config.cs"> + <Output TaskParameter="OutputFile" ItemName="Compile" /> + </WriteCodeFragment> + + </Target> +</Project> diff --git a/Tizen.Runtime/Tizen.Runtime.Mono/AssemblyManager.cs b/Tizen.Runtime/Tizen.Runtime.Mono/AssemblyManager.cs new file mode 100644 index 0000000..ec65ca6 --- /dev/null +++ b/Tizen.Runtime/Tizen.Runtime.Mono/AssemblyManager.cs @@ -0,0 +1,153 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Reflection; +using System.Runtime.InteropServices; +using Tizen.Runtime; + +namespace Tizen.Runtime.Mono +{ + public static class AssemblyManager + { + + private static SortedSet<string> ResolveDirectories = new SortedSet<string>(); + private static SortedSet<string> ResolveFiles = new SortedSet<string>(); + + public static bool Launch( + [In] string rootPath, + [In] string path, + [In] int argc, + [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)] + [In] string[] argv) + { + ALog.Debug($"Application Launch path : {path}"); + try + { + DirectoryInfo bindir = new DirectoryInfo(Path.Combine(rootPath, "bin")); + DirectoryInfo libdir = new DirectoryInfo(Path.Combine(rootPath, "lib")); + if (Directory.Exists(bindir.FullName)) + { + ResolveDirectories.Add(bindir.FullName); + } + if (Directory.Exists(libdir.FullName)) + { + ResolveDirectories.Add(libdir.FullName); + } + ResolveFiles.Add(path); + Execute(path, argv); + } + catch(Exception e) + { + ALog.Debug("Exception in Launch()"); + PrintException(e); + return false; + } + + return true; + } + + static readonly string[] assemblyExtensions = {".dll", ".exe"}; + + public static Assembly AssemblyResolverHandler(object sender, ResolveEventArgs args) + { + // find dll name + ext in paths for resolving + foreach (string path in ResolveDirectories) + { + foreach (string ext in assemblyExtensions) + { + string assemblyPath = Path.Combine(path, new AssemblyName(args.Name).Name + ext); + if (File.Exists(assemblyPath)) + { + Assembly assembly = Assembly.LoadFrom(assemblyPath); + return assembly; + } + } + } + + return null; + } + + public static void Prepared() + { + try + { + string preloadPath = ""; + ICustomAttributeProvider assembly = typeof(AssemblyManager).GetTypeInfo().Assembly; + var attributes = assembly.GetCustomAttributes(typeof(DefaultConfigAttribute), false); + foreach (DefaultConfigAttribute dca in attributes) + { + ALog.Debug($"{dca.Key} = {dca.Value}"); + if (dca.Key == "PreloadPath") + { + preloadPath = dca.Value; + } + } + + if (!Initialize(preloadPath)) + { + ALog.Debug($"Failed to Initialized with {preloadPath}"); + } + + AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolverHandler; + } + catch(Exception e) + { + ALog.Debug("Exception at Preparing"); + PrintException(e); + } + } + + private static void PrintException(Exception exception) + { + while (exception != null) + { + ALog.Debug(exception.ToString()); + exception = exception.InnerException; + } + } + + public static bool Initialize(string preloadDirectory) + { + try + { + if (!string.IsNullOrEmpty(preloadDirectory)) + { + Console.WriteLine($"[{preloadDirectory}]"); + DirectoryInfo d = new DirectoryInfo(preloadDirectory); + if (Directory.Exists(d.FullName)) + { + ResolveDirectories.Add(d.FullName); + } + } + } + catch (Exception e) + { + ALog.Debug("Exception on Initialized"); + PrintException(e); + return false; + } + return true; + } + + + public static void Execute(string dllPath, string[] argv) + { + try + { + FileInfo f = new FileInfo(dllPath); + if (File.Exists(f.FullName)) + { + Assembly asm = Assembly.LoadFile(f.FullName); + if (asm.EntryPoint == null) throw new ArgumentException($"{f.FullName} did not have EntryPoint"); + asm.EntryPoint.Invoke(null, new object[]{argv}); + } + } + catch (Exception e) + { + ALog.Debug("Exception on Execute"); + PrintException(e); + } + } + + } +} diff --git a/Tizen.Runtime/Tizen.Runtime/Ini.cs b/Tizen.Runtime/Tizen.Runtime/Ini.cs deleted file mode 100644 index d5c2544..0000000 --- a/Tizen.Runtime/Tizen.Runtime/Ini.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using System.IO; -using System.Collections.Generic; -using System.Linq; - -namespace Tizen.Runtime -{ - internal class Ini - { - public string GlobalName = "___GLOBAL"; - public Ini(Uri path) - { - if (path.Scheme != "file") - { - throw new NotSupportedException("Only File Uri is supported now"); - } - string absPath = path.AbsolutePath; - Initialize(); - Contents = File.ReadAllText(absPath); - } - - public Ini(string contents) - { - Initialize(); - Contents = contents; - } - - public string Contents - { - get { return contents; } - - set - { - contents = value; - ReadContents(contents); - } - } - - public Dictionary<string, string> this[string name] - { - get - { - if (groups.ContainsKey(name)) - return groups[name]; - return null; - } - } - - public override string ToString() - { - string str = ""; - - if (groups.ContainsKey(GlobalName)) - { - foreach (var kv in groups[GlobalName]) - { - str += $"{kv.Key} = {kv.Value}" + Environment.NewLine; - } - } - - foreach (var gkv in groups) - { - if (gkv.Key == GlobalName) continue; - str += $"[{gkv.Key}]" + Environment.NewLine; - foreach (var kv in gkv.Value) - { - str += $"{kv.Key} = {kv.Value}" + Environment.NewLine; - } - } - - return str; - } - - public Dictionary<string, string> Global - { - get - { - return groups[GlobalName]; - } - } - - private void Initialize() - { - groups = new Dictionary<string, Dictionary<string, string> >(); - groups[GlobalName] = new Dictionary<string, string>(); - } - - private string Uncommont(string str) - { - int comment_start = str.IndexOf('#'); - return str.Substring(0, comment_start); - } - - private bool GetHeader(string str, ref string groupName) - { - if (str.Length < 2 || str[0] != '[' || str[str.Length-1] != ']') return false; - string expected = str.Substring(1, str.Length-2); - groupName = expected.Trim(); - return true; - } - - private void GetKeyValueFromString(string str, out string key, out string value) - { - int eq_pos = str.IndexOf('='); - if (eq_pos == -1) - { - key = str.Trim(); - value = ""; - } - else - { - key = str.Substring(0, eq_pos).Trim(); - value = str.Substring(eq_pos+1).Trim(); - } - } - - private void ReadContents(string contents) - { - string key, value; - string groupName = GlobalName; - string[] lines = contents.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); - - foreach (string line in lines) - { - if (string.IsNullOrWhiteSpace(line)) continue; - if (GetHeader(line, ref groupName)) - { - groups[groupName] = new Dictionary<string, string>(); - continue; - } - GetKeyValueFromString(line, out key, out value); - groups[groupName][key] = value; - } - } - - private Dictionary<string, Dictionary<string, string> > groups; - private string contents; - } -} diff --git a/Tizen.Runtime/Tizen.Runtime/Interop.cs b/Tizen.Runtime/Tizen.Runtime/Interop.cs deleted file mode 100644 index 52ceeac..0000000 --- a/Tizen.Runtime/Tizen.Runtime/Interop.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -internal static class Interop -{ - public const string Launcher = "dotnet-launcher"; - - internal delegate int PreparedCallback(IntPtr userData); - internal delegate int RequestedCallback(IntPtr userData); - - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - internal delegate int ExecutedCallback( - [In] string path, - [In] string app_root, - [In] int argc, - [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)] - [In] string[] argv, - [In] IntPtr userData); - - [DllImport(Launcher)] - internal static extern void register_launching_callback( - [MarshalAs(UnmanagedType.FunctionPtr)] PreparedCallback prepared, - [MarshalAs(UnmanagedType.FunctionPtr)] RequestedCallback requested, - [MarshalAs(UnmanagedType.FunctionPtr)] ExecutedCallback executed, IntPtr userData); - - [DllImport(Launcher)] - internal static extern void wait_for_launching(int argc, string[] argv); -} diff --git a/Tizen.Runtime/Tizen.Runtime/Log.cs b/Tizen.Runtime/Tizen.Runtime/Log.cs index 064a5f3..b27a329 100644 --- a/Tizen.Runtime/Tizen.Runtime/Log.cs +++ b/Tizen.Runtime/Tizen.Runtime/Log.cs @@ -1,7 +1,7 @@ using System; -#if !CLOG -using Tizen; -#endif +using System.IO; +using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; namespace Tizen.Runtime { @@ -35,21 +35,54 @@ namespace Tizen.Runtime #endif internal static class ALog { - static string TAG = "Tizen.Runtime"; + const string Library = "libdlog.so.0"; + const string TAG = "Tizen.Runtime"; + + public static void Debug(string message, + [CallerFilePath] string file = "", + [CallerMemberName] string func = "", + [CallerLineNumber] int line = 0) + { + Print(LogPriority.DLOG_DEBUG, TAG, message, file, func, line); + } - public static void Debug(string message) + public static void Info(string message, + [CallerFilePath] string file = "", + [CallerMemberName] string func = "", + [CallerLineNumber] int line = 0) { - Log.Debug(TAG, message); + Print(LogPriority.DLOG_DEBUG, TAG, message, file, func, line); } - public static void Info(string message) + public static void Error(string message, + [CallerFilePath] string file = "", + [CallerMemberName] string func = "", + [CallerLineNumber] int line = 0) { - Log.Info(TAG, message); + Print(LogPriority.DLOG_DEBUG, TAG, message, file, func, line); } - public static void Error(string message) + internal enum LogPriority { - Log.Error(TAG, message); + DLOG_UNKNOWN = 0, + DLOG_DEFAULT, + DLOG_VERBOSE, + DLOG_DEBUG, + DLOG_INFO, + DLOG_WARN, + DLOG_ERROR, + DLOG_FATAL, + DLOG_SILENT, + DLOG_PRIO_MAX, } + + private static void Print(LogPriority priority, string tag, string message, string file, string func, int line) + { + FileInfo finfo = new FileInfo(file); + Print(priority, tag, "%s: %s(%d) > %s", finfo.Name, func, line, message); + } + + [DllImportAttribute(Library, EntryPoint = "dlog_print")] + internal static extern int Print(LogPriority prio, string tag, string fmt, string file, string func, int line, string msg); } } diff --git a/Tizen.Runtime/Tizen.Runtime/test.cs b/Tizen.Runtime/Tizen.Runtime/test.cs deleted file mode 100644 index 1c65dbe..0000000 --- a/Tizen.Runtime/Tizen.Runtime/test.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Tizen.Runtime; - -public class Program -{ - public static void Main(string[] args) - { - if (args.Length < 2) - { - ALog.Debug("Cancel.."); - return; - } - string preload = Environment.GetEnvironmentVariable("PRELOAD_DLLS"); - string searchable = String.Join(":", args, 1, args.Length - 1); - - if (AssemblyManager.Initialize(searchable, preload)) - { - ALog.Debug("After Initialized..."); - AssemblyManager.Execute(args[0]); - } - } -} |