瀏覽代碼

Build variadic functions as part of relibc

Jeremy Soller 7 年之前
父節點
當前提交
79d05d7eda
共有 9 個文件被更改,包括 103 次插入55 次删除
  1. 1 0
      Cargo.lock
  2. 3 0
      Cargo.toml
  3. 18 0
      build.rs
  4. 0 0
      include/assert.h
  5. 2 17
      include/bits/fcntl.h
  6. 4 35
      include/bits/stdio.h
  7. 3 3
      include/bits/string.h
  8. 24 0
      src/c/fcntl.c
  9. 48 0
      src/c/stdio.c

+ 1 - 0
Cargo.lock

@@ -263,6 +263,7 @@ dependencies = [
 name = "relibc"
 version = "0.1.0"
 dependencies = [
+ "cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
  "compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins.git)",
  "ctype 0.1.0",
  "errno 0.1.0",

+ 3 - 0
Cargo.toml

@@ -10,6 +10,9 @@ crate-type = ["staticlib"]
 [workspace]
 members = ["src/crt0"]
 
+[build-dependencies]
+cc = "1.0"
+
 [dependencies]
 compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] }
 ctype = { path = "src/ctype" }

+ 18 - 0
build.rs

@@ -0,0 +1,18 @@
+extern crate cc;
+
+use std::env;
+
+fn main() {
+    let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
+
+    cc::Build::new()
+        .flag("-nostdinc")
+    	.flag("-nostdlib")
+        .flag("-I").flag(&format!("{}/include", crate_dir))
+        .flag("-fno-stack-protector")
+        .file("src/c/fcntl.c")
+        .file("src/c/stdio.c")
+        .compile("relibc_c");
+
+    println!("cargo:rustc-link-lib=static=relibc_c");
+}

+ 0 - 0
include/assert.h


+ 2 - 17
include/bits/fcntl.h

@@ -1,22 +1,7 @@
 #ifndef _BITS_FCNTL_H
 #define _BITS_FCNTL_H
 
-int open(const char* filename, int flags, ...) {
-    mode_t mode = 0;
-    va_list ap;
-    va_start(ap, flags);
-    mode = va_arg(ap, mode_t);
-    va_end(ap);
-    return sys_open(filename, flags, mode);
-}
-
-int fcntl(int fildes, int cmd, ...) {
-    int args = 0;
-    va_list ap;
-    va_start(ap, cmd);
-    args = va_arg(ap, int);
-    va_end(ap);
-    return sys_fcntl(fildes, cmd, args);
-}
+int open(const char* filename, int flags, ...);
+int fcntl(int fildes, int cmd, ...);
 
 #endif

+ 4 - 35
include/bits/stdio.h

@@ -1,40 +1,9 @@
 #ifndef _BITS_STDIO_H
 #define _BITS_STDIO_H
 
-int fprintf(FILE * stream, const char * fmt, ...) {
-    int ret;
-    va_list ap;
-    va_start(ap, fmt);
-    ret = vfprintf(stream, fmt, ap);
-    va_end(ap);
-    return ret;
-}
-
-int printf(const char * fmt, ...) {
-    int ret;
-    va_list ap;
-    va_start(ap, fmt);
-    ret = vprintf(fmt, ap);
-    va_end(ap);
-    return ret;
-}
-
-int snprintf(char *s, size_t n, const char * fmt, ...) {
-    int ret;
-    va_list ap;
-    va_start(ap, fmt);
-    ret = vsnprintf(s, n, fmt, ap);
-    va_end(ap);
-    return ret;
-}
-
-int sprintf(char *s, const char * fmt, ...) {
-    int ret;
-    va_list ap;
-    va_start(ap, fmt);
-    ret = vsprintf(s, fmt, ap);
-    va_end(ap);
-    return ret;
-}
+int fprintf(FILE * stream, const char * fmt, ...);
+int printf(const char * fmt, ...);
+int snprintf(char *s, size_t n, const char * fmt, ...);
+int sprintf(char *s, const char * fmt, ...);
 
 #endif /* _BITS_STDIO_H */

+ 3 - 3
include/bits/string.h

@@ -1,9 +1,9 @@
-#ifndef _BITS_STDIO_H
-#define _BITS_STDIO_H
+#ifndef _BITS_STRING_H
+#define _BITS_STRING_H
 
 int memcmp(const void *s1, const void *s2, size_t n);
 void *memcpy(void *dest, const void *src, size_t n);
 void *memmove(void *dest, const void *src, size_t n);
 void *memset(void *s, int c, size_t n);
 
-#endif /* _BITS_STDIO_H */
+#endif /* _BITS_STRING_H */

+ 24 - 0
src/c/fcntl.c

@@ -0,0 +1,24 @@
+#include <stdarg.h>
+#include <sys/types.h>
+
+int sys_open(const char* filename, int flags, mode_t mode);
+
+int open(const char* filename, int flags, ...) {
+    mode_t mode = 0;
+    va_list ap;
+    va_start(ap, flags);
+    mode = va_arg(ap, mode_t);
+    va_end(ap);
+    return sys_open(filename, flags, mode);
+}
+
+int sys_fcntl(int fildes, int cmd, int args);
+
+int fcntl(int fildes, int cmd, ...) {
+    int args = 0;
+    va_list ap;
+    va_start(ap, cmd);
+    args = va_arg(ap, int);
+    va_end(ap);
+    return sys_fcntl(fildes, cmd, args);
+}

+ 48 - 0
src/c/stdio.c

@@ -0,0 +1,48 @@
+#include <stdarg.h>
+#include <stddef.h>
+
+typedef struct FILE FILE;
+
+int vfprintf(FILE * stream, const char * fmt, va_list ap);
+
+int fprintf(FILE * stream, const char * fmt, ...) {
+    int ret;
+    va_list ap;
+    va_start(ap, fmt);
+    ret = vfprintf(stream, fmt, ap);
+    va_end(ap);
+    return ret;
+}
+
+int vprintf(const char * fmt, va_list ap);
+
+int printf(const char * fmt, ...) {
+    int ret;
+    va_list ap;
+    va_start(ap, fmt);
+    ret = vprintf(fmt, ap);
+    va_end(ap);
+    return ret;
+}
+
+int vsnprintf(char * s, size_t n, const char * fmt, va_list ap);
+
+int snprintf(char * s, size_t n, const char * fmt, ...) {
+    int ret;
+    va_list ap;
+    va_start(ap, fmt);
+    ret = vsnprintf(s, n, fmt, ap);
+    va_end(ap);
+    return ret;
+}
+
+int vsprintf(char * s, const char * fmt, va_list ap);
+
+int sprintf(char *s, const char * fmt, ...) {
+    int ret;
+    va_list ap;
+    va_start(ap, fmt);
+    ret = vsprintf(s, fmt, ap);
+    va_end(ap);
+    return ret;
+}