ubisoft / Sharpmake

Sharpmake is an open-source C#-based solution for generating project definition files, such as Visual Studio projects and solutions, GNU makefiles, Xcode projects, etc.
Apache License 2.0
922 stars 168 forks source link

Custom argument doesn't work #315

Closed PierreCAMILLI closed 7 months ago

PierreCAMILLI commented 8 months ago

Hi,

I'm testing features offered by SharpMake but it seems custom arguments don't work as intended.

public class MyArguments
{
    [CommandLine.Option("hello", @"Hello World")]
    public void HelloWorld()
    {
        Console.WriteLine("Hello World!");
    }
}
public static class Main
{
    [Sharpmake.Main]
    public static void SharpmakeMain(Sharpmake.Arguments arguments)
    {
        // Tells Sharpmake to generate the solution described by
        MyArguments arg = new MyArguments();
        CommandLine.ExecuteOnObject(arg);
        arguments.Generate<SimpleLibrarySolution>();
    }
}

Executed with this command: Sharpmake.Application.exe /sources('main.sharpmake.cs') /verbose /generateDebugSolution /hello

This gives me the following output which complains about compatibility issues between arguments despite the ones indicated are the same:

[00:01]     generation done in 0.1 sec
[00:01]   Results:
[00:01]     projects  (    2 configurations)     0 generated,     2 up-to-date
[00:01]     solutions (    2 configurations)     0 generated,     1 up-to-date
[00:01]   time: 1.76 sec.
[00:01]   completed on 12/04/2023 16:29:16.
[00:02] [ERROR]C:\Users\pcamilli\AppData\Local\Temp\eccc4a5479f34973b627c55cd1d547f8.tmp.sharpmake.cs(7,10): error CS1001: Identifier expected
[00:02] 
Exception Error:
[00:02]     While running C:\Users\pcamilli\Projects\SharpMakeTest\LibraryTest\Sharpmake.Application.exe
    @12/04/2023 16:29:17: Exception message (level 0):
    Exception has been thrown by the target of an invocation.
    Inner exception message (level 1):
    Command line option '/hello' have invalid parameters '()', maybe not compatible with '()'
    C:\Users\pcamilli\AppData\Local\Temp\eccc4a5479f34973b627c55cd1d547f8.tmp.sharpmake.cs(7,10): error CS1001: Identifier expected

    Stack trace:
    Inner exception stack trace (level 1):
   at Sharpmake.CommandLine.Execute(Type type, Object instance, String commandLine, String[] namespaces) in /_/Sharpmake/CommandLine.cs:line 233
   at Sharpmake.CommandLine.ExecuteOnObject(Object obj, String commandLine, String[] namespaces) in /_/Sharpmake/CommandLine.cs:line 48
   at Sharpmake.CommandLine.ExecuteOnObject(Object obj, String commandLine) in /_/Sharpmake/CommandLine.cs:line 43
   at Sharpmake.CommandLine.ExecuteOnObject(Object obj) in /_/Sharpmake/CommandLine.cs:line 38
   at Main.SharpmakeMain(Arguments arguments) in C:\Users\pcamilli\Projects\SharpMakeTest\LibraryTest\main.sharpmake.cs:line 158
    Root stack trace (level 0):
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Sharpmake.Builder.ExecuteEntryPointInAssemblies[TEntryPoint](Assembly[] assemblies) in /_/Sharpmake/Builder.cs:line 346
   at Sharpmake.Application.Program.CreateBuilder(BaseBuildContext context, Argument parameters, Boolean allowCleanBlobs, Boolean generateDebugSolution) in /_/Sharpmake.Application/Program.cs:line 665
   at Sharpmake.Application.Program.CreateBuilderAndGenerate(BaseBuildContext buildContext, Argument parameters, Boolean generateDebugSolution) in /_/Sharpmake.Application/Program.cs:line 460
   at Sharpmake.Application.Program.Main() in /_/Sharpmake.Application/Program.cs:line 307

The same happens when I specify an argument:

public class MyArguments
{
    [CommandLine.Option("print", @"Print something")]
    public void PrintSomething(string str)
    {
        Console.WriteLine(str);
    }
}
[00:01]   time: 1.78 sec.
[00:01]   completed on 12/04/2023 16:38:51.
[00:02] [ERROR]C:\Users\pcamilli\AppData\Local\Temp\970a93a598194d77a1b8788ba9a5ccb6.tmp.sharpmake.cs(7,10): error CS1001: Identifier expected
[00:02] 
Exception Error:
[00:02]     While running C:\Users\pcamilli\Projects\SharpMakeTest\LibraryTest\Sharpmake.Application.exe
    @12/04/2023 16:38:52: Exception message (level 0):
    Exception has been thrown by the target of an invocation.
    Inner exception message (level 1):
    Command line option '/print' have invalid parameters '("Hello World!")', maybe not compatible with '(System.String str)'
    C:\Users\pcamilli\AppData\Local\Temp\970a93a598194d77a1b8788ba9a5ccb6.tmp.sharpmake.cs(7,10): error CS1001: Identifier expected

There also seem to be an issue with how temporary C# files are generated in my Temp folder. An anonymous namespace is generated:

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Sharpmake;

namespace 
{
    public static class AssemblerBuildFunction_Class
    {
        public static void AssemblerBuildFunction_Method(Object obj)
        {
            ((global::MyArguments)obj).HelloWorld();

        }
    }
}
jspelletier commented 8 months ago

Please try /print('hello') instead of double quotes for your print argument.

Also, no arguments options should work here is some code we have:

        [CommandLine.Option("nounittest", @"Disables unit tests.")]
        public static void CommandLineNoUnitTests()
        {
            UnitTest.Settings.Enabled = false;
        }
    We are calling sharpmake with /nounittest to activate this one.
sylvainfortin commented 7 months ago

I tried to reproduce your issue and it looks like its related to the fact that MyArgument is in the global namespace. If you move MyArguments class into a namespace. It fix the issue. So I believe the issue here is that Sharpmake command line parsing code is broken for object in the global namespace.

jspelletier commented 7 months ago

@PierreCAMILLI Does the answer from Sylvain solves your problem?

PierreCAMILLI commented 7 months ago

Hi,

I'm sorry, the scope changed on our project, we no longer work on this subject and I can't access the work previously done about it.

You can close this issue.

Thank you for your answers