changeset 1056:b6aa293729a5 draft

WIP: rust errno
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 20 Mar 2024 21:46:32 -0400
parents 5cc4c39212d3
children 48906c77fd87
files .hgignore CMakeLists.txt build.sh cmake/rust-errno.cmake rusty-bits.rs urldecode.rs
diffstat 6 files changed, 84 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Mar 17 16:46:13 2024 -0400
+++ b/.hgignore	Wed Mar 20 21:46:32 2024 -0400
@@ -12,6 +12,8 @@
 Testing
 mapfile-vers.gnu
 
+errno.rs
+
 sexpr.lex.c
 sexpr.tab.c
 sexpr.tab.h
--- a/CMakeLists.txt	Sun Mar 17 16:46:13 2024 -0400
+++ b/CMakeLists.txt	Wed Mar 20 21:46:32 2024 -0400
@@ -217,7 +217,17 @@
 		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/hg.cmake
 )
 
+add_custom_target(rust_errno ALL)
+
+add_custom_command(TARGET rust_errno
+	COMMAND ${CMAKE_COMMAND}
+		-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
+		-DBINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
+		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/rust-errno.cmake
+)
+
 add_dependencies(jeffpc revisiontag)
+add_dependencies(jeffpc rust_errno)
 
 #
 # CBOR file dumper
--- a/build.sh	Sun Mar 17 16:46:13 2024 -0400
+++ b/build.sh	Wed Mar 20 21:46:32 2024 -0400
@@ -1,9 +1,11 @@
 #!/bin/sh
 
 set -x
+set -e
 
+make rust_errno
 rustc --crate-type staticlib --crate-name jeffpc -o libjeffpcr.a --edition 2021 \
 	-A internal_features \
 	-C opt-level=3 -C prefer-dynamic=yes \
-	-g rusty-bits.rs && \
-	make
+	-g rusty-bits.rs
+make
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake/rust-errno.cmake	Wed Mar 20 21:46:32 2024 -0400
@@ -0,0 +1,64 @@
+#
+# Copyright (c) 2024 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+#
+
+execute_process(
+	COMMAND ${CMAKE_C_COMPILER} -dM -E /tmp/test.c
+	WORKING_DIRECTORY ${BINARY_DIR}
+	OUTPUT_VARIABLE out_var
+	RESULT_VARIABLE res_var
+)
+
+if(NOT ${res_var} EQUAL 0)
+	message(FATAL_ERROR "Failed to preprocess errno.h")
+endif()
+
+# split the output on newlines
+string(REPLACE "\n" ";" out_var "${out_var}")
+
+set(RUST_CODE "")
+set(ELAST "1000000")
+foreach(line ${out_var})
+	string(REGEX MATCH "^#define[ \t]+(E[A-Z0-9]+)[ \t]+([0-9]+)[ \t]*$" match "${line}")
+	if(NOT "${match}" STREQUAL "")
+		if("${CMAKE_MATCH_1}" STREQUAL "ELAST")
+			set(ELAST ${CMAKE_MATCH_2})
+		else()
+			set(RUST_CODE "${RUST_CODE}${CMAKE_MATCH_1} = ${CMAKE_MATCH_2},\n")
+		endif()
+	endif()
+endforeach()
+
+set(rstring "/* errno.rs - Generated by cmake. Changes will be lost! */\n"
+	"pub enum ERRNO {\n"
+	"${RUST_CODE}"
+	"}\n"
+	"#[allow(dead_code)]\n"
+	"pub const ELAST: i32 = ${ELAST}\;\n"
+)
+
+file(WRITE errno.rs.txt ${rstring})
+# copy the file to the final header only if the version changes
+# reduces needless rebuilds
+execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
+			${BINARY_DIR}/errno.rs.txt ${BINARY_DIR}/errno.rs)
+execute_process(COMMAND ${CMAKE_COMMAND} -E remove
+			${BINARY_DIR}/errno.rs.txt)
--- a/rusty-bits.rs	Sun Mar 17 16:46:13 2024 -0400
+++ b/rusty-bits.rs	Wed Mar 20 21:46:32 2024 -0400
@@ -60,6 +60,9 @@
     }};
 }
 
+mod errno;
+pub use errno::ERRNO;
+
 pub mod common;
 pub mod urldecode;
 
--- a/urldecode.rs	Sun Mar 17 16:46:13 2024 -0400
+++ b/urldecode.rs	Wed Mar 20 21:46:32 2024 -0400
@@ -45,12 +45,7 @@
  * and the '+' to space translation.
  */
 
-/* TODO: move to common errno module */
-enum ERRNO {
-    E2BIG = 7,
-    EINVAL = 22,
-    EILSEQ = 86
-}
+use super::ERRNO;
 
 enum CharState {
     /* simply copied the last char */