Can generate swift sources with custom target.

pull/330/head
Jussi Pakkanen 9 years ago
parent d531f915b1
commit 961b000090
  1. 48
      ninjabackend.py
  2. 10
      test cases/swift/4 generate/gen/main.swift
  3. 6
      test cases/swift/4 generate/gen/meson.build
  4. 4
      test cases/swift/4 generate/meson.build
  5. 3
      test cases/swift/4 generate/user/main.swift
  6. 2
      test cases/swift/4 generate/user/meson.build

@ -862,6 +862,26 @@ class NinjaBackend(backends.Backend):
result.append(self.get_target_filename(l))
return result
def split_swift_generated_sources(self, target):
all_srcs = []
for genlist in target.get_generated_sources():
if isinstance(genlist, build.CustomTarget):
for ifile in genlist.get_filename():
rel = os.path.join(self.get_target_dir(genlist), ifile)
all_srcs.append(rel)
else:
for ifile in genlist.get_outfilelist():
rel = os.path.join(self.get_target_private_dir(target), ifile)
all_srcs.append(rel)
srcs = []
others = []
for i in all_srcs:
if i.endswith('.swift'):
srcs.append(i)
else:
others.append(i)
return (srcs, others)
def generate_swift_target(self, target, outfile):
module_name = self.target_swift_modulename(target)
swiftc = self.environment.coredata.compilers['swift']
@ -873,15 +893,6 @@ class NinjaBackend(backends.Backend):
abss = os.path.normpath(os.path.join(self.environment.get_build_dir(), relsrc))
abssrc.append(abss)
os.makedirs(os.path.join(self.get_target_private_dir_abs(target)), exist_ok=True)
# We need absolute paths because swiftc needs to be invoked in a subdir
# and this is the easiest way about it.
objects = [] # Relative to swift invocation dir
rel_objects = [] # Relative to build.ninja
for i in abssrc:
base = os.path.split(i)[1]
oname = os.path.splitext(base)[0] + '.o'
objects.append(oname)
rel_objects.append(os.path.join(self.get_target_private_dir(target), oname))
compile_args = swiftc.get_compile_only_args()
compile_args += swiftc.get_module_args(module_name)
link_args = swiftc.get_output_args(os.path.join(self.environment.get_build_dir(), self.get_target_filename(target)))
@ -894,21 +905,32 @@ class NinjaBackend(backends.Backend):
module_includes += swiftc.get_include_args(x)
link_deps = self.get_swift_link_deps(target)
abs_link_deps = [os.path.join(self.environment.get_build_dir(), x) for x in link_deps]
(rel_generated, _) = self.split_swift_generated_sources(target)
abs_generated = [os.path.join(self.environment.get_build_dir(), x) for x in rel_generated]
# We need absolute paths because swiftc needs to be invoked in a subdir
# and this is the easiest way about it.
objects = [] # Relative to swift invocation dir
rel_objects = [] # Relative to build.ninja
for i in abssrc + abs_generated:
base = os.path.split(i)[1]
oname = os.path.splitext(base)[0] + '.o'
objects.append(oname)
rel_objects.append(os.path.join(self.get_target_private_dir(target), oname))
# Swiftc does not seem to be able to emit objects and module files in one go.
elem = NinjaBuildElement(rel_objects,
'swift_COMPILER',
abssrc)
elem.add_dep(in_module_files)
elem.add_item('ARGS', compile_args + module_includes)
elem.add_dep(in_module_files + rel_generated)
elem.add_item('ARGS', compile_args + abs_generated + module_includes)
elem.add_item('RUNDIR', rundir)
elem.write(outfile)
self.check_outputs(elem)
elem = NinjaBuildElement(out_module_name,
'swift_COMPILER',
abssrc)
elem.add_dep(in_module_files)
elem.add_item('ARGS', compile_args + module_includes + swiftc.get_mod_gen_args())
elem.add_dep(in_module_files + rel_generated)
elem.add_item('ARGS', compile_args + abs_generated + module_includes + swiftc.get_mod_gen_args())
elem.add_item('RUNDIR', rundir)
elem.write(outfile)
self.check_outputs(elem)

@ -0,0 +1,10 @@
import Glibc
let fname = Process.arguments[1]
let code = "public func getGenerated() -> Int {\n return 42\n}\n"
let f = fopen(fname, "w")
fwrite(code, 1, Int(strlen(code)), f)
print("Name: \(fname)")
fclose(f)

@ -0,0 +1,6 @@
gen = executable('gen', 'main.swift')
srcs = custom_target('gensrc',
output : 'gen.swift',
command : [gen, '@OUTPUT@']
)

@ -0,0 +1,4 @@
project('swift generator', 'swift')
subdir('gen')
subdir('user')

@ -0,0 +1,3 @@
let generated = getGenerated()
print("Generated number is: \(generated).")

@ -0,0 +1,2 @@
user = executable('user', 'main.swift', srcs)
test('User test', user)
Loading…
Cancel
Save