Merge pull request #2815 from taisei-project/fix_windows_compile_resources
[windows] make compile_resources use custom targets instead of generatorspull/2915/head
commit
a5b2b90309
9 changed files with 153 additions and 5 deletions
@ -0,0 +1,3 @@ |
||||
## Can use custom targets as Windows resource files |
||||
|
||||
The `compile_resources()` function of the `windows` module can now be used on custom targets as well as regular files. |
@ -0,0 +1,70 @@ |
||||
project('winmain', 'c') |
||||
|
||||
# MinGW windres has a bug due to which it doesn't parse args with space properly: |
||||
# https://github.com/mesonbuild/meson/pull/1346 |
||||
# https://sourceware.org/bugzilla/show_bug.cgi?id=4933 |
||||
if meson.get_compiler('c').get_id() == 'gcc' and host_machine.system() == 'windows' |
||||
# Construct build_to_src and skip this test if it has spaces |
||||
# because then the -I flag to windres will also have spaces |
||||
# and we know the test will fail |
||||
src_parts = meson.source_root().split('/') |
||||
build_parts = meson.build_root().split('/') |
||||
|
||||
# Get the common path (which might just be '/' or 'C:/') |
||||
common = [] |
||||
done = false |
||||
count = 0 |
||||
if src_parts.length() > build_parts.length() |
||||
parts = build_parts |
||||
other = src_parts |
||||
else |
||||
parts = src_parts |
||||
other = build_parts |
||||
endif |
||||
foreach part : parts |
||||
if not done and part == other.get(count) |
||||
common += [part] |
||||
else |
||||
done = true |
||||
endif |
||||
count += 1 |
||||
endforeach |
||||
|
||||
# Create path components to go down from the build root to the common path |
||||
count = 0 |
||||
rel = build_parts |
||||
foreach build : build_parts |
||||
if count < build_parts.length() - common.length() |
||||
rel += ['..'] |
||||
endif |
||||
count += 1 |
||||
endforeach |
||||
|
||||
# Create path components to go up from the common path to the build root |
||||
count = 0 |
||||
foreach src : src_parts |
||||
if count >= common.length() |
||||
rel += [src] |
||||
endif |
||||
count += 1 |
||||
endforeach |
||||
|
||||
build_to_src = '/'.join(rel) |
||||
|
||||
if build_to_src.contains(' ') |
||||
message('build_to_src is: ' + build_to_src) |
||||
error('MESON_SKIP_TEST build_to_src has spaces') |
||||
endif |
||||
# Welcome to the end of this conditional. |
||||
# We hope you never have to implement something like this. |
||||
endif |
||||
|
||||
subdir('res') |
||||
|
||||
foreach id : [0, 1, 2] |
||||
exe = executable('prog_@0@'.format(id), 'prog.c', |
||||
res[id], |
||||
gui_app : true) |
||||
|
||||
test('winmain_@0@'.format(id), exe) |
||||
endforeach |
@ -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(GetModuleHandle(NULL), MAKEINTRESOURCE(MY_ICON)); |
||||
return hIcon ? 0 : 1; |
||||
} |
@ -0,0 +1,6 @@ |
||||
#!/usr/bin/env python3 |
||||
|
||||
import sys |
||||
|
||||
with open(sys.argv[1], 'r') as infile, open(sys.argv[2], 'w') as outfile: |
||||
outfile.write(infile.read().format(icon=sys.argv[3])) |
@ -0,0 +1,18 @@ |
||||
win = import('windows') |
||||
|
||||
rc_writer = find_program('./gen-res.py') |
||||
|
||||
rc_sources = [] |
||||
|
||||
foreach id : [1, 2] |
||||
rc_sources += custom_target('RC source file @0@'.format(id), |
||||
input : 'myres.rc.in', |
||||
output : 'myres_@0@.rc'.format(id), |
||||
command : [rc_writer, '@INPUT@', '@OUTPUT@', files('sample.ico')], |
||||
install : false, |
||||
build_always : true) |
||||
endforeach |
||||
|
||||
rc_sources += files('myres_static.rc') |
||||
|
||||
res = win.compile_resources(rc_sources) |
@ -0,0 +1,3 @@ |
||||
#include<windows.h> |
||||
|
||||
1 ICON "{icon}" |
@ -0,0 +1,3 @@ |
||||
#include<windows.h> |
||||
|
||||
1 ICON "sample.ico" |
After Width: | Height: | Size: 9.4 KiB |
Loading…
Reference in new issue