From 37fad8b3dc2fb42ffd7175f185d26df37dd70d7b Mon Sep 17 00:00:00 2001 From: a Date: Thu, 22 Jun 2017 15:45:33 +0200 Subject: [PATCH] Add Rust support --- README.md | 49 ++++++++++------------------------------- configure.ac | 5 +++-- doc/pinkybar.1 | 52 +++++++++++--------------------------------- doc/pinkybar.texi | 60 ++++++++++++++++----------------------------------- m4/extend.m4 | 35 ++++++++++++++++++++++++++++-- src/Makefail.skel | 8 ++++--- src/options.c | 14 +++++++++++- src/pinky.rs | 20 +++++++++++++++++ src/prototypes/R.h | 2 +- src/prototypes/rust.h | 27 +++++++++++++++++++++++ src/rust.c | 40 ++++++++++++++++++++++++++++++++++ 11 files changed, 183 insertions(+), 129 deletions(-) create mode 100644 src/pinky.rs create mode 100644 src/prototypes/rust.h create mode 100644 src/rust.c diff --git a/README.md b/README.md index a6b6035..b390994 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ The code doesn't age, neither it has expiration date. - [Go lang](#go-lang) - [OCAML lang](#ocaml-lang) - [Rust lang](#rust-lang) +- [Assembly](#assembly) - [Wish list](#wish-list) --- @@ -817,53 +818,25 @@ pinky.ml \ ## Rust lang -By default I decided not to include Rust lang, but this doesn't mean you cannot do it by hand: +The source code that you should edit is in **src/pinky.rs**, but you'll have to edit **src/Makefail.skel** and add the following to: -**lib.rs** - -```rust -#![crate_type = "staticlib"] - -use std::os::raw::c_char; -use std::ffi::CString; - -#[no_mangle] -pub extern fn hello() -> *mut c_char { - let mut str = String::from("Hello World"); - - let c_str = CString::new(str).unwrap(); - c_str.into_raw() -} - -#[no_mangle] -pub extern fn Rfree(s: *mut c_char) { - unsafe { - if s.is_null() { return } - CString::from_raw(s) - }; -} +```bash +pinkybar_LDADD = pinky.a ``` -Compile with `rustc --crate-type=staticlib lib.rs` +Make sure that you do not use Assembly in the same time. -**test.c** +--- -```c -#include +## Assembly -extern char *hello(void); -extern void Rfree(char *); +The source code that you should edit is in **extra/scripts/pinky.s**, but you'll have to edit **src/Makefail.skel** and add the following to: -int main(void) { - char *str = hello(); - printf("%s\n", str); - Rfree(str); -} +```bash +pinkybar_SOURCES = extra/scripts/pinky.s ``` -Compile with `gcc liblib.a test.c -o test -lpthread -ldl` - ---- +Make sure that you do not use Rust in the same time. ## Wish list diff --git a/configure.ac b/configure.ac index 2307f6f..1b977e7 100644 --- a/configure.ac +++ b/configure.ac @@ -7,7 +7,7 @@ AC_CONFIG_HEADERS([src/config.h]) AM_INIT_AUTOMAKE([1.13 -Wall no-define foreign subdir-objects dist-xz no-dist-gzip std-options]) AM_SILENT_RULES([yes]) -m4_define([cuRos],[GENTOO]) m4_define([LINUKS], [cryMeAriver]) +{perl will replace this line} # With the addition of more runtime compile and # link tests, this option is no longer necessary. @@ -46,6 +46,7 @@ TEST_R TEST_ASSEMBLY TEST_ECL TEST_OCAML +TEST_RUST AC_CONFIG_FILES([ Makefile @@ -56,4 +57,4 @@ AC_OUTPUT echo echo 'Now type \"make\" and \"make install\" afterwards' -echo \ No newline at end of file +echo diff --git a/doc/pinkybar.1 b/doc/pinkybar.1 index 3ec5568..c4f736c 100644 --- a/doc/pinkybar.1 +++ b/doc/pinkybar.1 @@ -1653,59 +1653,31 @@ pinky.ml\ \\ * * * * * .SS Rust lang .PP -By default I decided not to include Rust lang, but this doesn't mean you -cannot do it by hand: -.PP -\f[B]lib.rs\f[] +The source code that you should edit is in \f[B]src/pinky.rs\f[], but +you'll have to edit \f[B]src/Makefail.skel\f[] and add the following to: .IP .nf \f[C] -#![crate_type\ =\ "staticlib"] - -use\ std::os::raw::c_char; -use\ std::ffi::CString; - -#[no_mangle] -pub\ extern\ fn\ hello()\ \->\ *mut\ c_char\ { -\ \ let\ mut\ str\ =\ String::from("Hello\ World"); - -\ \ let\ c_str\ =\ CString::new(str).unwrap(); -\ \ c_str.into_raw() -} - -#[no_mangle] -pub\ extern\ fn\ Rfree(s:\ *mut\ c_char)\ { -\ \ unsafe\ { -\ \ \ \ if\ s.is_null()\ {\ return\ } -\ \ \ \ CString::from_raw(s) -\ \ }; -} +pinkybar_LDADD\ =\ pinky.a \f[] .fi .PP -Compile with \f[C]rustc\ \-\-crate\-type=staticlib\ lib.rs\f[] +Make sure that you do not use Assembly in the same time. .PP -\f[B]test.c\f[] + * * * * * +.SS Assembly +.PP +The source code that you should edit is in +\f[B]extra/scripts/pinky.s\f[], but you'll have to edit +\f[B]src/Makefail.skel\f[] and add the following to: .IP .nf \f[C] -#include\ - -extern\ char\ *hello(void); -extern\ void\ Rfree(char\ *); - -int\ main(void)\ { -\ \ char\ *str\ =\ hello(); -\ \ printf("%s\\n",\ str); -\ \ Rfree(str); -} +pinkybar_SOURCES\ =\ extra/scripts/pinky.s \f[] .fi .PP -Compile with -\f[C]gcc\ liblib.a\ test.c\ \-o\ test\ \-lpthread\ \-ldl\f[] -.PP - * * * * * +Make sure that you do not use Rust in the same time. .SS Wish list .PP It would be great if I had *BSD compatible usb wifi dongle or wireless diff --git a/doc/pinkybar.texi b/doc/pinkybar.texi index 1c8b5f9..9bea8b7 100644 --- a/doc/pinkybar.texi +++ b/doc/pinkybar.texi @@ -97,6 +97,7 @@ cpu and motherboard sensors as @strong{modules} and not inlined. * Go lang:: * OCAML lang:: * Rust lang:: +* Assembly:: * Wish list:: @end menu @@ -1464,52 +1465,14 @@ pinky.ml \ @node Rust lang @section Rust lang @anchor{#rust-lang} -By default I decided not to include Rust lang, but this doesn't mean you -cannot do it by hand: - -@strong{lib.rs} +The source code that you should edit is in @strong{src/pinky.rs}, but +you'll have to edit @strong{src/Makefail.skel} and add the following to: @verbatim -#![crate_type = "staticlib"] - -use std::os::raw::c_char; -use std::ffi::CString; - -#[no_mangle] -pub extern fn hello() -> *mut c_char { - let mut str = String::from("Hello World"); - - let c_str = CString::new(str).unwrap(); - c_str.into_raw() -} - -#[no_mangle] -pub extern fn Rfree(s: *mut c_char) { - unsafe { - if s.is_null() { return } - CString::from_raw(s) - }; -} +pinkybar_LDADD = pinky.a @end verbatim -Compile with @code{rustc --crate-type=staticlib lib.rs} - -@strong{test.c} - -@verbatim -#include - -extern char *hello(void); -extern void Rfree(char *); - -int main(void) { - char *str = hello(); - printf("%s\n", str); - Rfree(str); -} -@end verbatim - -Compile with @code{gcc liblib.a test.c -o test -lpthread -ldl} +Make sure that you do not use Assembly in the same time. @iftex @bigskip@hrule@bigskip @@ -1518,6 +1481,19 @@ Compile with @code{gcc liblib.a test.c -o test -lpthread -ldl} ------------------------------------------------------------------------ @end ifnottex +@node Assembly +@section Assembly +@anchor{#assembly} +The source code that you should edit is in +@strong{extra/scripts/pinky.s}, but you'll have to edit +@strong{src/Makefail.skel} and add the following to: + +@verbatim +pinkybar_SOURCES = extra/scripts/pinky.s +@end verbatim + +Make sure that you do not use Rust in the same time. + @node Wish list @section Wish list @anchor{#wish-list} diff --git a/m4/extend.m4 b/m4/extend.m4 index af42bfa..84445c6 100644 --- a/m4/extend.m4 +++ b/m4/extend.m4 @@ -371,9 +371,9 @@ AC_DEFUN([TEST_ASSEMBLY],[ dnl TEST_ECL() function in configure.ac dnl -dnl Substitute ECL related linker +dnl Substitute ECL related linker flags dnl to the variable ECL_LIBS if -dnl the user enabled the --with-r switch +dnl the user enabled the --with-lisp switch AC_DEFUN([TEST_ECL],[ ECL_LIBS="" WITH_ECL=0 @@ -435,3 +435,34 @@ AC_DEFUN([TEST_OCAML],[ AC_SUBST(OCAML_CF) AC_DEFINE_UNQUOTED([WITH_OCAML],[$WITH_OCAML],[Extend the program via ocaml scripts]) ]) + + +dnl TEST_RUST() function in configure.ac +dnl +dnl Substitute ECL related linker flags +dnl to the variable ECL_LIBS if +dnl the user enabled the --with-rust switch +AC_DEFUN([TEST_RUST],[ + RUST_LIBS="" + WITH_RUST=0 + + AC_ARG_WITH([rust], + AS_HELP_STRING([--with-rust], + [Extend the program via rust scripts]), + [], + [with_rust=no] + ) + + AS_IF([test "x$with_rust" = "xyes"], [ + CHECK_CFLAGZ([-O0]) + + RUST_LIBS="-lpthread -ldl" + WITH_RUST=1 + + ]) + + AC_SUBST(RUST_LIBS) + AC_DEFINE_UNQUOTED([WITH_RUST],[$WITH_RUST],[Extend the program via rust scripts]) +]) + + diff --git a/src/Makefail.skel b/src/Makefail.skel index 31cf3ea..caed4ae 100644 --- a/src/Makefail.skel +++ b/src/Makefail.skel @@ -3,8 +3,7 @@ AM_CFLAGS = {amCF} \ bin_PROGRAMS = pinkybar -pinkybar_LDADD = $(X_LIBS) $(ALSA_LIBS) $(MPD_LIBS) $(PCI_LIBS) $(DVD_LIBS) $(SENSORS_LIBS) $(CURL_LIBS) $(LIBNL_LZ) $(PERL_LZ) $(LUA_LIBS) $(PYTHON_LZ) $(RUBY_LZ) $(R_LZ) $(ECL_LIBS) $(OCAML_LZ) {bzdlibs} - +pinkybar_LDADD = $(X_LIBS) $(ALSA_LIBS) $(MPD_LIBS) $(PCI_LIBS) $(DVD_LIBS) $(SENSORS_LIBS) $(CURL_LIBS) $(LIBNL_LZ) $(PERL_LZ) $(LUA_LIBS) $(PYTHON_LZ) $(RUBY_LZ) $(R_LZ) $(ECL_LIBS) $(OCAML_LZ) $(RUST_LIBS) {bzdlibs} pinkybar_SOURCES = main.c \ ocaml.c \ ecl.c \ @@ -18,7 +17,7 @@ pinkybar_SOURCES = main.c \ perl.c \ lua.c \ ruby.c \ - ../extra/scripts/pinky.s \ + rust.c \ python.c \ R.c \ prototypes/ecl.h \ @@ -46,3 +45,6 @@ pinkybar_SOURCES = main.c \ .ml.o: ocamlc -output-obj $< -o $@ + +pinky.a: + rustc --crate-type=staticlib pinky.rs -o pinky.a diff --git a/src/options.c b/src/options.c index 468ebf6..8d9a133 100644 --- a/src/options.c +++ b/src/options.c @@ -35,6 +35,7 @@ #include "prototypes/R.h" #include "prototypes/ecl.h" #include "prototypes/ocaml.h" +#include "prototypes/rust.h" /* Because we ran out of a-z A-Z options, * only long ones will be supported from now on. @@ -52,6 +53,7 @@ enum { ASSEMBLY, LISP, OCAML, + RUST, BULLSHIFT }; const char *argp_program_version = PACKAGE_STRING; @@ -133,9 +135,13 @@ static const struct argp_option options[] = { #endif /* WITH_ECL */ #if WITH_OCAML == 1 - { .name = "ocaml", .key = OCAML, .doc = "Extend the program with lisp, read README." }, + { .name = "ocaml", .key = OCAML, .doc = "Extend the program with ocaml, read README." }, #endif /* WITH_OCAML */ +#if WITH_RUST == 1 + { .name = "rust", .key = RUST, .doc = "Extend the program with rust, read README." }, +#endif /* WITH_RUST */ + #if WITH_WEATHER == 1 { .name = "weather", .key = 'q', .arg = "London,uk", .doc = "The temperature outside." }, #endif /* WITH_WEATHER */ @@ -304,11 +310,17 @@ parse_opt(int key, char *arg, struct argp_state *state) { NEW_ARG_LABEL(LISP, char ecl[VLA], ecl, FMT_KERN); #endif /* WITH_ECL */ + #if WITH_OCAML == 1 NEW_LABEL(OCAML, char ocaml[VLA], ocaml, FMT_KERN); #endif /* WITH_OCAML */ +#if WITH_RUST == 1 + NEW_LABEL(RUST, char rust[VLA], rust, FMT_KERN); +#endif /* WITH_RUST */ + + #if WITH_WEATHER == 1 NEW_ARG_LABEL('q', char weather[VLA], weather, OUT_STR YELLOW STR_SPEC" "); #endif /* WITH_WEATHER */ diff --git a/src/pinky.rs b/src/pinky.rs new file mode 100644 index 0000000..bfff06e --- /dev/null +++ b/src/pinky.rs @@ -0,0 +1,20 @@ +#![crate_type = "staticlib"] + +use std::os::raw::c_char; +use std::ffi::CString; + +#[no_mangle] +pub extern fn uzer_func() -> *mut c_char { + let mut str = String::from("Hello World"); + + let c_str = CString::new(str).unwrap(); + c_str.into_raw() +} + +#[no_mangle] +pub extern fn Rfree(s: *mut c_char) { + unsafe { + if s.is_null() { return } + CString::from_raw(s) + }; +} diff --git a/src/prototypes/R.h b/src/prototypes/R.h index ab8cd9f..9cff90f 100644 --- a/src/prototypes/R.h +++ b/src/prototypes/R.h @@ -1,5 +1,5 @@ /* - 06/16/2016 + 06/16/2017 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/src/prototypes/rust.h b/src/prototypes/rust.h new file mode 100644 index 0000000..c72dea7 --- /dev/null +++ b/src/prototypes/rust.h @@ -0,0 +1,27 @@ +/* + 06/22/2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. +*/ + +#ifndef RUST_H_ +#define RUST_H_ + +#if WITH_RUST == 1 +void get_rust(char *); +#endif /* WITH_RUST */ + +#endif /* RUST_H_ */ diff --git a/src/rust.c b/src/rust.c new file mode 100644 index 0000000..4d316f6 --- /dev/null +++ b/src/rust.c @@ -0,0 +1,40 @@ +/* + 06/22/2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. +*/ + +#include "config.h" /* Auto-generated */ + +#if WITH_RUST == 1 +#include "include/headers.h" +#endif /* WITH_RUST */ + +#include "prototypes/rust.h" + +#if WITH_RUST == 1 +extern char *uzer_func(void); +extern void Rfree(char *); + +void +get_rust(char *str1) { + char *str = uzer_func(); + FILL_STR_ARR(1, str1, str); + Rfree(str); +} +#else +char *rust9; +#endif /* WITH_RUST */ -- 2.11.0