demake 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/demake +438 -492
- data/lib/data/example/demake/applications +3 -0
- data/lib/data/example/demake/license +19 -0
- data/lib/data/example/demake/settings.rb +62 -0
- data/lib/data/example/demake/test-target.rb +8 -0
- data/lib/data/example/src/goodbye.c +12 -0
- data/lib/data/example/src/hello.c +12 -0
- data/lib/data/example/src/string/string.c +12 -0
- data/lib/data/example/src/string/string.h +7 -0
- data/lib/data/oreo/demake/applications +1 -0
- data/lib/data/oreo/demake/license +19 -0
- data/lib/data/oreo/demake/settings.rb +62 -0
- data/lib/data/oreo/demake/test-target.rb +9 -0
- data/lib/data/oreo/oreo_test.txt +1 -0
- data/lib/data/oreo/src/defines.h +29 -0
- data/lib/data/oreo/src/fast_read_file.h +259 -0
- data/lib/data/oreo/src/oreo.c +102 -0
- data/lib/data/oreo/src/typedefs.h +61 -0
- data/lib/template/build_target.rb +6 -0
- data/lib/template/clean_target.rb +6 -0
- data/lib/template/debug_executable_target.rb +14 -0
- data/lib/template/debug_library_target.rb +14 -0
- data/lib/template/debug_target.rb +4 -0
- data/lib/template/dependency_targets.rb +22 -0
- data/lib/template/executable_debug_target.rb +6 -0
- data/lib/template/executable_target.rb +14 -0
- data/lib/template/generic_dependency_targets.rb +26 -0
- data/lib/template/library_debug_target.rb +6 -0
- data/lib/template/library_target.rb +6 -0
- data/lib/template/license_target.rb +10 -0
- data/lib/template/link_library_target.rb +14 -0
- data/lib/template/strip_build.rb +8 -0
- metadata +37 -5
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Uncomment to use / override
|
|
2
|
+
# # Default Setting | none = ""
|
|
3
|
+
#@silent = true # false
|
|
4
|
+
#@strip = false # true
|
|
5
|
+
#@debug_enabled = false # true
|
|
6
|
+
#@compiler = "clang" # "gcc"
|
|
7
|
+
#@num_threads = 16 # 8
|
|
8
|
+
#@prefix = "/usr/local/bin64/" # "/usr/local/bin/"
|
|
9
|
+
#@library_prefix = "/usr/local/lib64/" # "/usr/local/lib/"
|
|
10
|
+
#
|
|
11
|
+
# ** demake never makes changes **
|
|
12
|
+
#
|
|
13
|
+
#@source_directory = "source" # "src"
|
|
14
|
+
#
|
|
15
|
+
# ** make clean deletes all files in these directories **
|
|
16
|
+
#
|
|
17
|
+
#@binary_directory = "binary" # "bin"
|
|
18
|
+
#@library_directory = "library" # "lib"
|
|
19
|
+
#@object_directory = "object" # "obj"
|
|
20
|
+
#
|
|
21
|
+
#@emojis = false # true
|
|
22
|
+
#@replace_with = "|b*|n" # "|mo|n"
|
|
23
|
+
# Both compiler & linker
|
|
24
|
+
@flags = "-ansi -pedantic"
|
|
25
|
+
@optimize_flags = "-O2"
|
|
26
|
+
#@compiler_flags = "-fPIE"
|
|
27
|
+
#@linker_flags = "-fPIE"
|
|
28
|
+
#@linker_libraries = "-lc"
|
|
29
|
+
# Platform based
|
|
30
|
+
#@linux_flags = "-static"
|
|
31
|
+
#@linux_flags = "-static -static-libgcc"
|
|
32
|
+
#@x86_64_flags = "-msse2 -mssse3 -msse4.1 -msha"
|
|
33
|
+
#@aarch64_flags = "-march=armv8-a+crypto"
|
|
34
|
+
#@riscv64_flags = "-march=native"
|
|
35
|
+
#@windows_flags = "-target x86_64-w64-windows-gnu"
|
|
36
|
+
#@freebsd_flags = ""
|
|
37
|
+
#@darwin_flags = "-bundle"
|
|
38
|
+
# Both compiler & linker
|
|
39
|
+
#@libraries = "-lc"
|
|
40
|
+
#@libraries = "-I/usr/include/ruby-3.4"
|
|
41
|
+
# Used for building libraries
|
|
42
|
+
#@library_compiler_flags = "-fPIC"
|
|
43
|
+
#@library_linker_flags = "-fPIC"
|
|
44
|
+
#@library_linker_libraries = "-lruby340"
|
|
45
|
+
# Debugging
|
|
46
|
+
@debug_flags = "-DEXAMPLE_DEBUG -g -Wall -Wextra -Wunused-variable -Wundef " +
|
|
47
|
+
"-Wconversion -Wshadow -Wcast-qual -Wwrite-strings"
|
|
48
|
+
=begin
|
|
49
|
+
@debug_flags << "-Wpedantic -Wdeprecated-declarations -Wdiv-by-zero " +
|
|
50
|
+
"-Wimplicit-function-declaration -Wimplicit-int " +
|
|
51
|
+
"-Wpointer-arith -Wwrite-strings -Wold-style-definition " +
|
|
52
|
+
"-Wmissing-noreturn -Wno-cast-function-type " +
|
|
53
|
+
"-Wno-constant-logical-operand -Wno-long-long " +
|
|
54
|
+
"-Wno-missing-field-initializers -Wno-overlength-strings " +
|
|
55
|
+
"-Wno-packed-bitfield-compat -Wno-parentheses-equality " +
|
|
56
|
+
"-Wno-self-assign -Wno-tautological-compare " +
|
|
57
|
+
"-Wno-unused-parameter -Wno-unused-value " +
|
|
58
|
+
"-Wsuggest-attribute=format -Wsuggest-attribute=noreturn"
|
|
59
|
+
=end
|
|
60
|
+
# Manually include (relative to Makefile) other raw binaries in linking
|
|
61
|
+
#@raw_binary_files = "myfile/hello.o myfile/goodbye.o"
|
|
62
|
+
notify("#{@emoji_success} demake/settings.rb is present")
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The most basic multiple executable example using a shared object/header file.
|
|
3
|
+
|
|
4
|
+
goodbye.c will generate an executable named hello which that prints goodbye, world
|
|
5
|
+
*/
|
|
6
|
+
#include "string/string.h"
|
|
7
|
+
|
|
8
|
+
int main(void)
|
|
9
|
+
{
|
|
10
|
+
out("goodbye, world");
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The most basic multiple executable example using a shared object/header file.
|
|
3
|
+
|
|
4
|
+
hello.c will generate an executable named hello which that prints hello, world
|
|
5
|
+
*/
|
|
6
|
+
#include "string/string.h"
|
|
7
|
+
|
|
8
|
+
int main(void)
|
|
9
|
+
{
|
|
10
|
+
out("hello, world");
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The most basic multiple executable example using a shared object/header file.
|
|
3
|
+
|
|
4
|
+
string.c creates a simple shared function named out which prints output
|
|
5
|
+
*/
|
|
6
|
+
#include <stdio.h>
|
|
7
|
+
#include "string.h"
|
|
8
|
+
|
|
9
|
+
void out(const char *in)
|
|
10
|
+
{
|
|
11
|
+
printf("%s\n", in);
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
oreo
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Uncomment to use / override
|
|
2
|
+
# # Default Setting | none = ""
|
|
3
|
+
#@silent = true # false
|
|
4
|
+
#@strip = false # true
|
|
5
|
+
#@debug_enabled = false # true
|
|
6
|
+
#@compiler = "clang" # "gcc"
|
|
7
|
+
#@num_threads = 16 # 8
|
|
8
|
+
#@prefix = "/usr/local/bin64/" # "/usr/local/bin/"
|
|
9
|
+
#@library_prefix = "/usr/local/lib64/" # "/usr/local/lib/"
|
|
10
|
+
#
|
|
11
|
+
# ** demake never makes changes **
|
|
12
|
+
#
|
|
13
|
+
#@source_directory = "source" # "src"
|
|
14
|
+
#
|
|
15
|
+
# ** make clean deletes all files in these directories **
|
|
16
|
+
#
|
|
17
|
+
#@binary_directory = "binary" # "bin"
|
|
18
|
+
#@library_directory = "library" # "lib"
|
|
19
|
+
#@object_directory = "object" # "obj"
|
|
20
|
+
#
|
|
21
|
+
#@emojis = false # true
|
|
22
|
+
#@replace_with = "|b*|n" # "|mo|n"
|
|
23
|
+
# Both compiler & linker
|
|
24
|
+
@flags = "-ansi -pedantic -D_POSIX_C_SOURCE=200809L"
|
|
25
|
+
@optimize_flags = "-O2"
|
|
26
|
+
#@compiler_flags = "-fPIE"
|
|
27
|
+
#@linker_flags = "-fPIE"
|
|
28
|
+
#@linker_libraries = "-lc"
|
|
29
|
+
# Platform based
|
|
30
|
+
#@linux_flags = "-static"
|
|
31
|
+
#@linux_flags = "-static -static-libgcc"
|
|
32
|
+
#@x86_64_flags = "-msse2 -mssse3 -msse4.1 -msha"
|
|
33
|
+
#@aarch64_flags = "-march=armv8-a+crypto"
|
|
34
|
+
#@riscv64_flags = "-march=native"
|
|
35
|
+
#@windows_flags = "-target x86_64-w64-windows-gnu"
|
|
36
|
+
#@freebsd_flags = ""
|
|
37
|
+
#@darwin_flags = "-bundle"
|
|
38
|
+
# Both compiler & linker
|
|
39
|
+
#@libraries = "-lc"
|
|
40
|
+
#@libraries = "-I/usr/include/ruby-3.4"
|
|
41
|
+
# Used for building libraries
|
|
42
|
+
#@library_compiler_flags = "-fPIC"
|
|
43
|
+
#@library_linker_flags = "-fPIC"
|
|
44
|
+
#@library_linker_libraries = "-lruby340"
|
|
45
|
+
# Debugging
|
|
46
|
+
@debug_flags = "-DOREO_DEBUG -g -Wall -Wextra -Wunused-variable -Wundef " +
|
|
47
|
+
"-Wconversion -Wshadow -Wcast-qual -Wwrite-strings"
|
|
48
|
+
=begin
|
|
49
|
+
@debug_flags << "-Wpedantic -Wdeprecated-declarations -Wdiv-by-zero " +
|
|
50
|
+
"-Wimplicit-function-declaration -Wimplicit-int " +
|
|
51
|
+
"-Wpointer-arith -Wwrite-strings -Wold-style-definition " +
|
|
52
|
+
"-Wmissing-noreturn -Wno-cast-function-type " +
|
|
53
|
+
"-Wno-constant-logical-operand -Wno-long-long " +
|
|
54
|
+
"-Wno-missing-field-initializers -Wno-overlength-strings " +
|
|
55
|
+
"-Wno-packed-bitfield-compat -Wno-parentheses-equality " +
|
|
56
|
+
"-Wno-self-assign -Wno-tautological-compare " +
|
|
57
|
+
"-Wno-unused-parameter -Wno-unused-value " +
|
|
58
|
+
"-Wsuggest-attribute=format -Wsuggest-attribute=noreturn"
|
|
59
|
+
=end
|
|
60
|
+
# Manually include (relative to Makefile) other raw binaries in linking
|
|
61
|
+
#@raw_binary_files = "myfile/hello.o myfile/goodbye.o"
|
|
62
|
+
notify("#{@emoji_success} demake/settings.rb is present")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Test 5
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
defines.h -- Basic C preprocessor macro definitions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#ifndef DEFINES_H_Minaswan /* Header guard to prevent multiple inclusions */
|
|
6
|
+
#define DEFINES_H_Minaswan
|
|
7
|
+
#define DEFINES_VERSION "0.1.0"
|
|
8
|
+
#ifndef NULL
|
|
9
|
+
#define NULL ((void *) 0)
|
|
10
|
+
#endif
|
|
11
|
+
#ifndef FALSE
|
|
12
|
+
#define FALSE 0
|
|
13
|
+
#endif
|
|
14
|
+
#ifndef TRUE
|
|
15
|
+
#define TRUE 1
|
|
16
|
+
#endif
|
|
17
|
+
#ifndef NO
|
|
18
|
+
#define NO 0
|
|
19
|
+
#endif
|
|
20
|
+
#ifndef YES
|
|
21
|
+
#define YES 1
|
|
22
|
+
#endif
|
|
23
|
+
#ifndef OFF
|
|
24
|
+
#define OFF 0
|
|
25
|
+
#endif
|
|
26
|
+
#ifndef ON
|
|
27
|
+
#define ON 1
|
|
28
|
+
#endif
|
|
29
|
+
#endif /* DEFINES_H_Minaswan */
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
fast_read_file.h -- *Full Library* Header File
|
|
4
|
+
|
|
5
|
+
For reading the entire contents of a file into 64-bit aligned memory.
|
|
6
|
+
|
|
7
|
+
See the end of this file for examples.
|
|
8
|
+
|
|
9
|
+
Public Functions: ** DON'T FORGET TO FREE YOUR MEMORY AFTER USE **
|
|
10
|
+
|
|
11
|
+
c8 *fast_read_all_file_mapped(FILE *file, u64 *total_bytes); ** READ ONLY **
|
|
12
|
+
void free_all_mapped_read_memory(c8 *data, u64 *total_bytes);
|
|
13
|
+
|
|
14
|
+
c8 *fast_read_all_file_aligned(FILE *file, u64 *total_bytes); ** READ/WRITE **
|
|
15
|
+
void free_all_aligned_read_memory(c8 *data);
|
|
16
|
+
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#ifndef FAST_READ_FILE_H_Minaswan /* Prevent multiple inclusions */
|
|
20
|
+
#define FAST_READ_FILE_H_Minaswan
|
|
21
|
+
#define FAST_READ_FILE_VERSION "0.1.0"
|
|
22
|
+
#ifndef TYPEDEFS_H_Minaswan /* Header guard for typedefs.h */
|
|
23
|
+
#define TYPEDEFS_H_Minaswan
|
|
24
|
+
#include <stdint.h>
|
|
25
|
+
typedef uint8_t b8; /* Booleans */
|
|
26
|
+
typedef uint16_t b16;
|
|
27
|
+
typedef uint32_t b32;
|
|
28
|
+
typedef uint64_t b64;
|
|
29
|
+
|
|
30
|
+
typedef char c8; /* Characters */
|
|
31
|
+
typedef unsigned char uc8;
|
|
32
|
+
|
|
33
|
+
typedef uint8_t u8; /* Unsigned Numbers */
|
|
34
|
+
typedef uint16_t u16;
|
|
35
|
+
typedef uint32_t u32;
|
|
36
|
+
typedef uint64_t u64;
|
|
37
|
+
|
|
38
|
+
typedef int8_t i8; /* Signed Numbers */
|
|
39
|
+
typedef int16_t i16;
|
|
40
|
+
typedef int32_t i32;
|
|
41
|
+
typedef int64_t i64;
|
|
42
|
+
|
|
43
|
+
typedef float f32; /* Floating Point Numbers */
|
|
44
|
+
typedef double f64;
|
|
45
|
+
#endif /* TYPEDEFS_H_Minaswan */
|
|
46
|
+
|
|
47
|
+
#include <stdio.h>
|
|
48
|
+
|
|
49
|
+
/* Public Functions */
|
|
50
|
+
void free_all_mapped_read_memory(c8 *data, u64 *total_bytes);
|
|
51
|
+
void free_all_aligned_read_memory(c8 *data);
|
|
52
|
+
c8 *fast_read_all_file_mapped(FILE *file, u64 *total_bytes);
|
|
53
|
+
c8 *fast_read_all_file_aligned(FILE *file, u64 *total_bytes);
|
|
54
|
+
|
|
55
|
+
#ifdef FAST_READ_FILE_IMPLEMENTATION /* Effectively read_all_file.c */
|
|
56
|
+
#include <stdlib.h>
|
|
57
|
+
#include <string.h>
|
|
58
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
59
|
+
#include <windows.h>
|
|
60
|
+
#else
|
|
61
|
+
#include <sys/mman.h>
|
|
62
|
+
#include <sys/stat.h>
|
|
63
|
+
#include <fcntl.h>
|
|
64
|
+
#include <unistd.h>
|
|
65
|
+
#endif
|
|
66
|
+
|
|
67
|
+
extern i32 fileno(FILE *stream);
|
|
68
|
+
|
|
69
|
+
#define FAST_READ_FILE_BUFFER_SIZE 4096
|
|
70
|
+
|
|
71
|
+
#ifndef FALSE
|
|
72
|
+
#define FALSE 0
|
|
73
|
+
#endif
|
|
74
|
+
|
|
75
|
+
#ifndef TRUE
|
|
76
|
+
#define TRUE 1
|
|
77
|
+
#endif
|
|
78
|
+
|
|
79
|
+
#ifndef NULL
|
|
80
|
+
#define NULL ((void *) 0)
|
|
81
|
+
#endif
|
|
82
|
+
|
|
83
|
+
#ifndef MIN
|
|
84
|
+
#define MIN(a, b) ((a) < (b) ? a : b)
|
|
85
|
+
#endif
|
|
86
|
+
|
|
87
|
+
void *fast_file_align64_malloc(size_t size)
|
|
88
|
+
{
|
|
89
|
+
void *result = NULL;
|
|
90
|
+
|
|
91
|
+
if(size == 0)
|
|
92
|
+
return(NULL);
|
|
93
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
94
|
+
result = _aligned_malloc(size, 64);
|
|
95
|
+
#else
|
|
96
|
+
#if(defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
|
|
97
|
+
(defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
|
|
98
|
+
if(posix_memalign(&result, 64, size) != 0)
|
|
99
|
+
return(NULL);
|
|
100
|
+
#else
|
|
101
|
+
result = malloc(size);
|
|
102
|
+
#endif
|
|
103
|
+
#endif
|
|
104
|
+
return(result);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void *fast_file_align64_calloc(size_t size)
|
|
108
|
+
{
|
|
109
|
+
void *result = fast_file_align64_malloc(size);
|
|
110
|
+
|
|
111
|
+
if(result)
|
|
112
|
+
memset(result, 0, size);
|
|
113
|
+
return(result);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void *fast_file_align64_realloc(void *existing_memory, size_t old_size, size_t new_size)
|
|
117
|
+
{
|
|
118
|
+
void *new_memory = fast_file_align64_calloc(new_size);
|
|
119
|
+
|
|
120
|
+
if(new_memory && existing_memory) {
|
|
121
|
+
memcpy(new_memory, existing_memory, MIN(new_size, old_size));
|
|
122
|
+
free(existing_memory);
|
|
123
|
+
return(new_memory);
|
|
124
|
+
}
|
|
125
|
+
return(NULL);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
c8 *fast_read_all_file_aligned(FILE *file, u64 *total_bytes)
|
|
129
|
+
{
|
|
130
|
+
size_t bytes = 0, count = 0;
|
|
131
|
+
c8 buffer[FAST_READ_FILE_BUFFER_SIZE + 1];
|
|
132
|
+
c8 *data = NULL;
|
|
133
|
+
|
|
134
|
+
bytes = fread(buffer, sizeof(c8), FAST_READ_FILE_BUFFER_SIZE, file);
|
|
135
|
+
while(bytes) {
|
|
136
|
+
if(data) {
|
|
137
|
+
data = fast_file_align64_realloc(data, *total_bytes + 1, *total_bytes + bytes + 1);
|
|
138
|
+
count = 0;
|
|
139
|
+
while(count < bytes) {
|
|
140
|
+
*(data + *total_bytes + count) = buffer[count];
|
|
141
|
+
count++;
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
data = fast_file_align64_calloc(bytes + 1);
|
|
145
|
+
count = 0;
|
|
146
|
+
while(count < bytes) {
|
|
147
|
+
*(data + *total_bytes + count) = buffer[count];
|
|
148
|
+
count++;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
*total_bytes += (u32)bytes;
|
|
152
|
+
*(data + *total_bytes) = '\0';
|
|
153
|
+
bytes = fread(buffer, sizeof(c8), FAST_READ_FILE_BUFFER_SIZE, file);
|
|
154
|
+
}
|
|
155
|
+
return(data);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
c8 *fast_read_all_file_mapped(FILE *file, u64 *total_bytes)
|
|
159
|
+
{
|
|
160
|
+
c8 *data = NULL;
|
|
161
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
162
|
+
HANDLE windows_file_handle = {0};
|
|
163
|
+
HANDLE windows_map_handle = {0};
|
|
164
|
+
LARGE_INTEGER windows_file_size = {0};
|
|
165
|
+
#else
|
|
166
|
+
i32 file_descriptor = fileno(file);
|
|
167
|
+
struct stat st = {0};
|
|
168
|
+
#endif
|
|
169
|
+
|
|
170
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
171
|
+
windows_file_handle = (HANDLE)_get_osfhandle(fileno(file));
|
|
172
|
+
if(windows_file_handle == INVALID_HANDLE_VALUE)
|
|
173
|
+
return(NULL);
|
|
174
|
+
if(!GetFileSizeEx(windows_file_handle, &windows_file_size) ||
|
|
175
|
+
windows_file_size.QuadPart == 0) {
|
|
176
|
+
CloseHandle(windows_file_handle);
|
|
177
|
+
return(NULL);
|
|
178
|
+
}
|
|
179
|
+
*total_bytes = (u64)windows_file_size.QuadPart;
|
|
180
|
+
windows_map_handle = CreateFileMapping(windows_file_handle,
|
|
181
|
+
NULL, PAGE_READONLY, 0, 0, NULL);
|
|
182
|
+
data = MapViewOfFile(windows_map_handle, FILE_MAP_READ, 0, 0, 0);
|
|
183
|
+
CloseHandle(windows_file_handle);
|
|
184
|
+
CloseHandle(windows_map_handle);
|
|
185
|
+
#else
|
|
186
|
+
if(file_descriptor == -1)
|
|
187
|
+
return(NULL);
|
|
188
|
+
if(fstat(file_descriptor, &st) == -1 || st.st_size == 0) {
|
|
189
|
+
close(file_descriptor);
|
|
190
|
+
return(NULL);
|
|
191
|
+
}
|
|
192
|
+
*total_bytes = (u64)st.st_size;
|
|
193
|
+
data = mmap(NULL, *total_bytes, PROT_READ, MAP_PRIVATE,
|
|
194
|
+
file_descriptor, 0);
|
|
195
|
+
if(data == MAP_FAILED) {
|
|
196
|
+
close(file_descriptor);
|
|
197
|
+
return(NULL);
|
|
198
|
+
}
|
|
199
|
+
#ifdef POSIX_FADV_SEQUENTIAL
|
|
200
|
+
posix_fadvise(file_descriptor, 0, (i64)*total_bytes,
|
|
201
|
+
POSIX_FADV_SEQUENTIAL);
|
|
202
|
+
#endif
|
|
203
|
+
#ifdef MADV_SEQUENTIAL
|
|
204
|
+
madvise(data, *total_bytes, MADV_SEQUENTIAL);
|
|
205
|
+
#endif
|
|
206
|
+
close(file_descriptor);
|
|
207
|
+
#endif
|
|
208
|
+
return(data);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
void free_all_mapped_read_memory(c8 *data, u64 *total_bytes)
|
|
212
|
+
{
|
|
213
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
214
|
+
UnmapViewOfFile(data);
|
|
215
|
+
#else
|
|
216
|
+
munmap(data, *total_bytes);
|
|
217
|
+
#endif
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
void free_all_aligned_read_memory(c8 *data)
|
|
221
|
+
{
|
|
222
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
223
|
+
_aligned_free(data);
|
|
224
|
+
#else
|
|
225
|
+
free(data);
|
|
226
|
+
#endif
|
|
227
|
+
}
|
|
228
|
+
#endif /* FAST_READ_FILE_IMPLEMENTATION */
|
|
229
|
+
#endif /* FAST_READ_FILE_H_Minaswan */
|
|
230
|
+
|
|
231
|
+
/*
|
|
232
|
+
|
|
233
|
+
Examples: ** DON'T FORGET TO FREE YOUR MEMORY AFTER USE **
|
|
234
|
+
|
|
235
|
+
#define FAST_READ_FILE_IMPLEMENTATION
|
|
236
|
+
#include "fast_read_file.h"
|
|
237
|
+
|
|
238
|
+
// Read everything from file example:
|
|
239
|
+
|
|
240
|
+
FILE *file = fopen(argv[1], "rb");
|
|
241
|
+
u64 total_bytes = 0;
|
|
242
|
+
c8 *data = fast_read_all_file_mapped(file, &total_bytes);
|
|
243
|
+
use(string);
|
|
244
|
+
free_all_mapped_read_memory(string); // Note: Doesn't use malloc/free
|
|
245
|
+
|
|
246
|
+
// Read everything from stdin / non-kernal mapping example:
|
|
247
|
+
|
|
248
|
+
u64 total_bytes = 0;
|
|
249
|
+
c8 *data = fast_read_all_file_aligned(stdin, &total_bytes);
|
|
250
|
+
use(string);
|
|
251
|
+
free_all_aligned_read_memory(string); // Note: Might not use malloc/free
|
|
252
|
+
|
|
253
|
+
FILE *file = fopen(argv[1], "rb");
|
|
254
|
+
u64 total_bytes = 0;
|
|
255
|
+
c8 *data = fast_read_all_file_aligned(file, &total_bytes);
|
|
256
|
+
use(string);
|
|
257
|
+
free_all_aligned_read_memory(string); // Note: Might not use malloc/free
|
|
258
|
+
|
|
259
|
+
*/
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/*
|
|
2
|
+
oreo.c -- A simple c program that reads data piped in, issued as arguments or from a file and echos output
|
|
3
|
+
|
|
4
|
+
Program main entry point: _start/main
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
|
|
11
|
+
/* Cross platform test to see if data has been piped from stdin */
|
|
12
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
13
|
+
#include <io.h>
|
|
14
|
+
#define piped_from_stdin !_isatty(fileno(stdin))
|
|
15
|
+
#else
|
|
16
|
+
#include <unistd.h>
|
|
17
|
+
#define piped_from_stdin !isatty(fileno(stdin))
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
/* Cross platform test to see if data has been piped from stdin - Option 2 */
|
|
21
|
+
/*
|
|
22
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
23
|
+
#include <windows.h>
|
|
24
|
+
#else
|
|
25
|
+
#include <sys/ioctl.h>
|
|
26
|
+
#endif
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
#include "defines.h"
|
|
30
|
+
#include "typedefs.h"
|
|
31
|
+
#define FAST_READ_FILE_IMPLEMENTATION
|
|
32
|
+
#include "fast_read_file.h"
|
|
33
|
+
|
|
34
|
+
i32 main(i32 argc, c8 **argv)
|
|
35
|
+
{
|
|
36
|
+
FILE *file = NULL;
|
|
37
|
+
u64 total_bytes = 0, l = 0;
|
|
38
|
+
i32 n = 0;
|
|
39
|
+
c8 *string = NULL;
|
|
40
|
+
b8 displayed = FALSE;
|
|
41
|
+
|
|
42
|
+
/* Cross platform test to see if data has been piped from stdin - Option 2 */
|
|
43
|
+
/*
|
|
44
|
+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
|
45
|
+
HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
|
|
46
|
+
DWORD bytes;
|
|
47
|
+
|
|
48
|
+
if(PeekNamedPipe(stdin_handle, NULL, 0, NULL, &bytes, NULL) && bytes > 0) {
|
|
49
|
+
#else
|
|
50
|
+
if(ioctl(0, FIONREAD, &n) == 0 && n > 0) {
|
|
51
|
+
#endif
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
if(piped_from_stdin) {
|
|
55
|
+
string = (c8 *)fast_read_all_file_aligned(stdin, &total_bytes);
|
|
56
|
+
#ifdef OREO_DEBUG
|
|
57
|
+
printf("From stdin:\r\n");
|
|
58
|
+
#endif
|
|
59
|
+
printf("oreo: %s\r\n", string);
|
|
60
|
+
free_all_aligned_read_memory(string);
|
|
61
|
+
string = NULL;
|
|
62
|
+
displayed = TRUE;
|
|
63
|
+
}
|
|
64
|
+
if(argv[1] != NULL) {
|
|
65
|
+
file = fopen(argv[1], "r");
|
|
66
|
+
if(file == NULL) {
|
|
67
|
+
/* We need to manually recreate the command input based on arguments with spaces */
|
|
68
|
+
for(n = 1; n < argc; n++) {
|
|
69
|
+
if(string != NULL) {
|
|
70
|
+
l = (u32)strlen(argv[n]);
|
|
71
|
+
total_bytes += l + 1; /* +1 for space */
|
|
72
|
+
string = realloc(string, (size_t)total_bytes + 1); /* +1 for \0 */
|
|
73
|
+
strncat((c8 *)string, " ", (size_t)2); /* 2 for space and \0 */
|
|
74
|
+
strncat((c8 *)string, argv[n], l);
|
|
75
|
+
} else {
|
|
76
|
+
total_bytes = (u32)strlen(argv[n]);
|
|
77
|
+
string = calloc(total_bytes + 1, sizeof(c8)); /* +1 for \0 */
|
|
78
|
+
strncpy((c8 *)string, argv[n], total_bytes);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
#ifdef OREO_DEBUG
|
|
82
|
+
printf("From argument:\r\n");
|
|
83
|
+
#endif
|
|
84
|
+
printf("oreo: %s\r\n", string);
|
|
85
|
+
free(string);
|
|
86
|
+
string = NULL;
|
|
87
|
+
} else {
|
|
88
|
+
#ifdef OREO_DEBUG
|
|
89
|
+
printf("From file: %s\r\n", argv[1]);
|
|
90
|
+
#endif
|
|
91
|
+
string = (c8 *)fast_read_all_file_mapped(file, &total_bytes);
|
|
92
|
+
printf("oreo: %s\r\n", string);
|
|
93
|
+
free_all_mapped_read_memory(string, &total_bytes);
|
|
94
|
+
string = NULL;
|
|
95
|
+
}
|
|
96
|
+
} else if(!displayed) {
|
|
97
|
+
#ifdef OREO_DEBUG
|
|
98
|
+
printf("No input provided.\r\n");
|
|
99
|
+
#endif
|
|
100
|
+
}
|
|
101
|
+
exit(0);
|
|
102
|
+
}
|