From 7c4590b81801bde3cfd7f2fd0919c01c41a78024 Mon Sep 17 00:00:00 2001
From: Matheus Catarino <matheus-catarino@hotmail.com>
Date: Thu, 22 Aug 2024 14:15:29 -0300
Subject: [PATCH] hello_swift added required: swift 6 (embedded module)

---
 Application.mk                              | 30 ++++++++++++++---
 Make.defs                                   |  1 +
 examples/hello_swift/BridgingHeader.h       | 28 ++++++++++++++++
 examples/hello_swift/Kconfig                | 29 ++++++++++++++++
 examples/hello_swift/Make.defs              | 23 +++++++++++++
 examples/hello_swift/Makefile               | 37 +++++++++++++++++++++
 examples/hello_swift/hello_swift_main.swift | 33 ++++++++++++++++++
 7 files changed, 177 insertions(+), 4 deletions(-)
 create mode 100644 examples/hello_swift/BridgingHeader.h
 create mode 100644 examples/hello_swift/Kconfig
 create mode 100644 examples/hello_swift/Make.defs
 create mode 100644 examples/hello_swift/Makefile
 create mode 100644 examples/hello_swift/hello_swift_main.swift

diff --git a/Application.mk b/Application.mk
index 803fff642..5e5a68446 100644
--- a/Application.mk
+++ b/Application.mk
@@ -77,27 +77,30 @@ CXXOBJS = $(CXXSRCS:=$(SUFFIX)$(OBJEXT))
 RUSTOBJS = $(RUSTSRCS:=$(SUFFIX)$(OBJEXT))
 ZIGOBJS = $(ZIGSRCS:=$(SUFFIX)$(OBJEXT))
 DOBJS = $(DSRCS:=$(SUFFIX)$(OBJEXT))
+SWIFTOBJS = $(SWIFTSRCS:=$(SUFFIX)$(OBJEXT))
 
 MAINCXXSRCS = $(filter %$(CXXEXT),$(MAINSRC))
 MAINCSRCS = $(filter %.c,$(MAINSRC))
 MAINRUSTSRCS = $(filter %$(RUSTEXT),$(MAINSRC))
 MAINZIGSRCS = $(filter %$(ZIGEXT),$(MAINSRC))
 MAINDSRCS = $(filter %$(DEXT),$(MAINSRC))
+MAINSWIFTSRCS = $(filter %$(SWIFTEXT),$(MAINSRC))
 MAINCXXOBJ = $(MAINCXXSRCS:=$(SUFFIX)$(OBJEXT))
 MAINCOBJ = $(MAINCSRCS:=$(SUFFIX)$(OBJEXT))
 MAINRUSTOBJ = $(MAINRUSTSRCS:=$(SUFFIX)$(OBJEXT))
 MAINZIGOBJ = $(MAINZIGSRCS:=$(SUFFIX)$(OBJEXT))
 MAINDOBJ = $(MAINDSRCS:=$(SUFFIX)$(OBJEXT))
+MAINSWIFTOBJ = $(MAINSWIFTSRCS:=$(SUFFIX)$(OBJEXT))
 
 SRCS = $(ASRCS) $(CSRCS) $(CXXSRCS) $(MAINSRC)
-OBJS = $(RAOBJS) $(CAOBJS) $(COBJS) $(CXXOBJS) $(RUSTOBJS) $(ZIGOBJS) $(DOBJS) $(EXTOBJS)
+OBJS = $(RAOBJS) $(CAOBJS) $(COBJS) $(CXXOBJS) $(RUSTOBJS) $(ZIGOBJS) $(DOBJS) $(SWIFTOBJS) $(EXTOBJS)
 
 ifneq ($(BUILD_MODULE),y)
-  OBJS += $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ) $(MAINDOBJ)
+  OBJS += $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ) $(MAINDOBJ) $(MAINSWIFTOBJ)
 endif
 
 ifneq ($(strip $(PROGNAME)),)
-  PROGOBJ := $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ) $(MAINDOBJ)
+  PROGOBJ := $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ) $(MAINDOBJ) $(MAINSWIFTOBJ)
   PROGLIST := $(addprefix $(BINDIR)$(DELIM),$(PROGNAME))
   REGLIST := $(addprefix $(BUILTIN_REGISTRY)$(DELIM),$(addsuffix .bdat,$(PROGNAME)))
 
@@ -135,6 +138,7 @@ endif
 ZIGELFFLAGS ?= $(ZIGFLAGS)
 RUSTELFFLAGS ?= $(RUSTFLAGS)
 DELFFLAGS ?= $(DFLAGS)
+SWIFTELFFLAGS ?= $(SWIFTFLAGS)
 
 DEPPATH += --dep-path .
 DEPPATH += --obj-path .
@@ -185,6 +189,12 @@ define ELFCOMPILED
 	$(ECHO_END)
 endef
 
+define ELFCOMPILESWIFT
+	$(ECHO_BEGIN)"SWIFTC: $1 "
+	$(Q) $(SWIFTC) -c $(SWIFTELFFLAGS) $($(strip $1)_SWIFTELFFLAGS) $1 -o $2
+	$(ECHO_END)
+endef
+
 define ELFLD
 	$(ECHO_BEGIN)"LD: $2 "
 	$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) $(ARCHCRT0OBJ) $1 $(LDSTARTGROUP) $(LDLIBS) $(LDENDGROUP) -o $2
@@ -226,6 +236,10 @@ $(DOBJS): %$(DEXT)$(SUFFIX)$(OBJEXT): %$(DEXT)
 	$(if $(and $(CONFIG_BUILD_LOADABLE), $(CELFFLAGS)), \
 		$(call ELFCOMPILED, $<, $@), $(call COMPILED, $<, $@))
 
+$(SWIFTOBJS): %$(SWIFTEXT)$(SUFFIX)$(OBJEXT): %$(SWIFTEXT)
+	$(if $(and $(CONFIG_BUILD_LOADABLE), $(CELFFLAGS)), \
+		$(call ELFCOMPILESWIFT, $<, $@), $(call COMPILESWIFT, $<, $@))
+
 AROBJS :=
 ifneq ($(OBJS),)
 SORTOBJS := $(sort $(OBJS))
@@ -264,7 +278,11 @@ $(MAINDOBJ): %$(DEXT)$(SUFFIX)$(OBJEXT): %$(DEXT)
 	$(if $(and $(CONFIG_BUILD_LOADABLE),$(CELFFLAGS)), \
 		$(call ELFCOMPILED, $<, $@), $(call COMPILED, $<, $@))
 
-$(PROGLIST): $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ) $(MAINDOBJ)
+$(MAINSWIFTOBJ): %$(SWIFTEXT)$(SUFFIX)$(OBJEXT): %$(SWIFTEXT)
+	$(if $(and $(CONFIG_BUILD_LOADABLE),$(CELFFLAGS)), \
+		$(call ELFCOMPILESWIFT, $<, $@), $(call COMPILESWIFT, $<, $@))
+
+$(PROGLIST): $(MAINCOBJ) $(MAINCXXOBJ) $(MAINRUSTOBJ) $(MAINZIGOBJ) $(MAINDOBJ) $(MAINSWIFTOBJ)
 	$(Q) mkdir -p $(BINDIR)
 	$(call ELFLD, $(PROGOBJ_$@), $(call CONVERT_PATH,$@))
 	$(Q) chmod +x $@
@@ -303,6 +321,10 @@ $(MAINDOBJ): %$(DEXT)$(SUFFIX)$(OBJEXT): %$(DEXT)
 	$(if $(and $(CONFIG_BUILD_LOADABLE),$(CELFFLAGS)), \
 		$(call ELFCOMPILED, $<, $@), $(call COMPILED, $<, $@))
 
+$(MAINSWIFTOBJ): %$(SWIFTEXT)$(SUFFIX)$(OBJEXT): %$(SWIFTEXT)
+	$(if $(and $(CONFIG_BUILD_LOADABLE),$(CELFFLAGS)), \
+		$(call ELFCOMPILESWIFT, $<, $@), $(call COMPILESWIFT, $<, $@))
+
 install::
 	@:
 
diff --git a/Make.defs b/Make.defs
index 4f97aa804..427ebd72d 100644
--- a/Make.defs
+++ b/Make.defs
@@ -55,6 +55,7 @@ CXXEXT ?= .cxx
 RUSTEXT ?= .rs
 ZIGEXT ?= .zig
 DEXT ?= .d
+SWIFTEXT ?= .swift
 
 # Library path
 
diff --git a/examples/hello_swift/BridgingHeader.h b/examples/hello_swift/BridgingHeader.h
new file mode 100644
index 000000000..4d5ed3d94
--- /dev/null
+++ b/examples/hello_swift/BridgingHeader.h
@@ -0,0 +1,28 @@
+/****************************************************************************
+ * apps/examples/hello_swift/BridgingHeader.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+#ifndef BridgingHeader_h
+#define BridgingHeader_h
+#include <nuttx/config.h>
+#include <stdio.h>
+#endif // BridgingHeader_h
\ No newline at end of file
diff --git a/examples/hello_swift/Kconfig b/examples/hello_swift/Kconfig
new file mode 100644
index 000000000..4bdeac8ab
--- /dev/null
+++ b/examples/hello_swift/Kconfig
@@ -0,0 +1,29 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_HELLO_SWIFT
+	tristate "\"Hello, Swift!\" Swift embedded example"
+	default n
+	---help---
+		Enable the \"Hello, Swift!\" Swift embedded example
+
+if EXAMPLES_HELLO_SWIFT
+
+config EXAMPLES_HELLO_SWIFT_PROGNAME
+	string "Program name"
+	default "hello_swift"
+	---help---
+		This is the name of the program that will be used when the
+		program is installed.
+
+config EXAMPLES_HELLO_SWIFT_PRIORITY
+	int "Hello Swift task priority"
+	default 100
+
+config EXAMPLES_HELLO_SWIFT_STACKSIZE
+	int "Hello Swift stack size"
+	default DEFAULT_TASK_STACKSIZE
+
+endif
diff --git a/examples/hello_swift/Make.defs b/examples/hello_swift/Make.defs
new file mode 100644
index 000000000..2829de01e
--- /dev/null
+++ b/examples/hello_swift/Make.defs
@@ -0,0 +1,23 @@
+############################################################################
+# apps/examples/hello_swift/Make.defs
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you 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.
+#
+############################################################################
+
+ifneq ($(CONFIG_EXAMPLES_HELLO_SWIFT),)
+CONFIGURED_APPS += $(APPDIR)/examples/hello_swift
+endif
diff --git a/examples/hello_swift/Makefile b/examples/hello_swift/Makefile
new file mode 100644
index 000000000..5f6a39be8
--- /dev/null
+++ b/examples/hello_swift/Makefile
@@ -0,0 +1,37 @@
+############################################################################
+# apps/examples/hello_swift/Makefile
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you 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.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# Hello, World! Swift Embedded Example
+
+MAINSRC = hello_swift_main.swift
+
+# hello_swift built-in application info
+
+SWIFTFLAGS += -import-bridging-header BridgingHeader.h -I$(TOPDIR)/include
+SWIFTFLAGS += -Xfrontend -function-sections -Xfrontend -disable-stack-protector
+SWIFTFLAGS += -Xfrontend -enable-single-module-llvm-emission
+PROGNAME  = $(CONFIG_EXAMPLES_HELLO_SWIFT_PROGNAME)
+PRIORITY  = $(CONFIG_EXAMPLES_HELLO_SWIFT_PRIORITY)
+STACKSIZE = $(CONFIG_EXAMPLES_HELLO_SWIFT_STACKSIZE)
+MODULE    = $(CONFIG_EXAMPLES_HELLO_SWIFT)
+
+include $(APPDIR)/Application.mk
diff --git a/examples/hello_swift/hello_swift_main.swift b/examples/hello_swift/hello_swift_main.swift
new file mode 100644
index 000000000..215235e50
--- /dev/null
+++ b/examples/hello_swift/hello_swift_main.swift
@@ -0,0 +1,33 @@
+/****************************************************************************
+ * apps/examples/hello_swift/hello_swift_main.swift
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: hello_swift_main
+ ****************************************************************************/
+func swiftMain() {
+    print("Hello from Swift embedded!")
+}
+
+// Create a C-compatible entry point
+@_cdecl("hello_swift_main")
+public func cMain(_ argc: Int32, _ argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>) -> Int32 {
+    swiftMain()
+    return 0
+}
\ No newline at end of file