Repository: mesonbuild/meson

Issue:
 We need win module and a method win.compile_resources() 
VC compile resource to COFF with 2 tools - 'RC', 'CVTRES'.

> Compiling resource files using the Win32 SDK requires three separate tools: the first tool you use is 
> the RC compiler, which turns your .RC file into a .RES file; you then use the CVTRES tool to convert
> the .RES file into an object file; and finally, you link the object file to the program executable using
> LINK32.
> https://msdn.microsoft.com/en-us/library/cc194805.aspx
> 
> You can specify a .res file when linking a program. The .res file is created by the resource compiler
> (RC). LINK automatically converts .res files to COFF. The CVTRES.exe tool must be in the same
> directory as LINK.exe or in a directory specified in the PATH environment variable.
> https://msdn.microsoft.com/en-us/library/kcz8ck35.aspx

But mingw have some other tool 'windres.exe' which combinate 'RC.exe' and 'CVTRES.exe' in one executable. 
http://www.mingw.org/wiki/MS_resource_compiler

Can we have some functional for easier compiling resource with vc compiler ?



Discussion:

Resolving patch:
diff --git a/compilers.py b/compilers.py
--- a/compilers.py
+++ b/compilers.py
@@ -27,7 +27,7 @@
 cpp_suffixes = ['cc', 'cpp', 'cxx', 'h', 'hh', 'hpp', 'hxx', 'c++']
 c_suffixes = ['c']
 clike_suffixes = c_suffixes + cpp_suffixes
-obj_suffixes = ['o', 'obj']
+obj_suffixes = ['o', 'obj', 'res']
 
 def is_header(fname):
     if hasattr(fname, 'fname'):
diff --git a/modules/windows.py b/modules/windows.py
new file mode 100644
--- /dev/null
+++ b/modules/windows.py
@@ -0,0 +1,47 @@
+# Copyright 2015 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+#     http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import mesonlib, dependencies, build
+from coredata import MesonException
+import os
+
+class WindowsModule:
+
+    def detect_compiler(self, compilers):
+        for c in compilers:
+            if c.language == 'c' or c.language == 'cpp':
+                return c
+        raise MesonException('Resource compilation requires a C or C++ compiler.')
+
+    def compile_resources(self, state, args, kwargs):
+        comp = self.detect_compiler(state.compilers)
+        extra_args = mesonlib.stringlistify(kwargs.get('args', []))
+        if comp.id == 'msvc':
+            rescomp = dependencies.ExternalProgram('rc', silent=True)
+            res_args = extra_args + ['/nologo', '/fo@OUTPUT@', '@INPUT@']
+            suffix = 'res'
+        else:
+            rescomp = dependencies.ExternalProgram('windres', silent=True)
+            res_args = extra_args + ['@INPUT@', '@OUTPUT@']
+            suffix = 'o'
+        res_files = mesonlib.stringlistify(args)
+        res_kwargs = {'output' : '@BASENAME@.' + suffix,
+                      'arguments': res_args}
+        res_gen = build.Generator([rescomp], res_kwargs)
+        res_output = build.GeneratedList(res_gen)
+        [res_output.add_file(os.path.join(state.subdir, a)) for a in res_files]
+        return res_output
+
+def initialize():
+    return WindowsModule()


Test patch:
diff --git a/test cases/windows/5 resources/meson.build b/test cases/windows/5 resources/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/windows/5 resources/meson.build	
@@ -0,0 +1,9 @@
+project('winmain', 'c')
+
+win = import('windows')
+
+exe = executable('prog', 'prog.c',
+  win.compile_resources('myres.rc'),
+  gui_app : true)
+
+test('winmain', exe)
diff --git a/test cases/windows/5 resources/myres.rc b/test cases/windows/5 resources/myres.rc
new file mode 100644
--- /dev/null
+++ b/test cases/windows/5 resources/myres.rc	
@@ -0,0 +1,3 @@
+#include<windows.h>
+
+1 ICON "sample.ico"
diff --git a/test cases/windows/5 resources/prog.c b/test cases/windows/5 resources/prog.c
new file mode 100644
--- /dev/null
+++ b/test cases/windows/5 resources/prog.c	
@@ -0,0 +1,14 @@
+#include<windows.h>
+
+#define MY_ICON 1
+
+int APIENTRY
+WinMain(
+    HINSTANCE hInstance,
+    HINSTANCE hPrevInstance,
+    LPSTR lpszCmdLine,
+    int nCmdShow) {
+    HICON hIcon;
+    hIcon = LoadIcon(NULL, IDI_APPLICATION);
+    return hIcon ? 0 : 1;
+}
diff --git a/test cases/windows/5 resources/sample.ico b/test cases/windows/5 resources/sample.ico
new file mode 100644
Binary files /dev/null and b/test cases/windows/5 resources/sample.ico differ