Skip to content

Commit 34a7c12

Browse files
committed
j4a: output helper file
1 parent 5520b84 commit 34a7c12

31 files changed

+148
-66
lines changed

Makefile

+16-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ CPPFLAGS = -std=c++11 -I./src -Wno-deprecated-register
33
YACC_FLAGS = --debug --verbose -d
44
YYLEX_FLAGS =
55
J4A = ./j4a
6+
ROOT_CLASS_INCLUDES = jni/j4a/j4a_allclasses.include.h
7+
ROOT_CLASS_LOADERS = jni/j4a/j4a_allclasses.loader.h
68

79
all: j4a test
810

@@ -88,16 +90,26 @@ $(TEST_C_SRCS): jni/j4a/class/%.c: test/i_java/%.java
8890
ifneq ("$<", "jni/j4a/class/.c")
8991
@mkdir -p $(shell dirname $@)
9092
$(J4A) -c $< -o $@
93+
@cat jni/j4a/class/$*.include.j4a >> $(ROOT_CLASS_INCLUDES)
94+
@echo >> $(ROOT_CLASS_INCLUDES)
95+
@cat jni/j4a/class/$*.loader.j4a >> $(ROOT_CLASS_LOADERS)
96+
@echo >> $(ROOT_CLASS_LOADERS)
9197
@#cp $@ test/ref_c/$*.c
9298
@#cp jni/j4a/class/$*.h test/ref_c/$*.h
93-
@diff test/ref_c/$*.c $@
94-
@diff test/ref_c/$*.h jni/j4a/class/$*.h
99+
@diff test/ref_c/$*.c $@
100+
@diff test/ref_c/$*.h jni/j4a/class/$*.h
101+
@diff test/ref_c/$*.include.j4a jni/j4a/class/$*.include.j4a
102+
@diff test/ref_c/$*.loader.j4a jni/j4a/class/$*.loader.j4a
95103
endif
96104

97-
test: resettest j4a $(TEST_C_SRCS)
105+
test: j4a resettest $(TEST_C_SRCS)
98106

99107
resettest:
100108
@rm -f $(TEST_C_SRCS)
109+
@rm -f $(TEST_CLASS_INCLUDES)
110+
@rm -f $(TEST_CLASS_LOADERS)
111+
@rm -f $(ROOT_CLASS_INCLUDES)
112+
@rm -f $(ROOT_CLASS_LOADERS)
101113
@mkdir -p jni
102114
@cp -r include/* jni/
103115

@@ -114,7 +126,7 @@ install: j4a
114126
# -----
115127
.PHONY: all test clean install
116128

117-
clean:
129+
clean: resettest
118130
rm -f $(CXX_OBJS)
119131
rm -f $(CXX_DEPS)
120132
rm -f j4a

include/j4a/j4a_allclasses.c

-54
This file was deleted.

include/j4a/j4a_base.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919
*/
2020

21-
#ifndef J4A_INTERNAL_H
22-
#define J4A_INTERNAL_H
21+
#ifndef J4A_BASE_H
22+
#define J4A_BASE_H
2323

2424
#include <string.h>
2525
#include <stdbool.h>

jni/j4a/j4a_allclasses.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* copyright (c) 2015 Zhang Rui <[email protected]>
3+
*
4+
* This file is part of jni4android.
5+
*
6+
* jni4android is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* jni4android is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with jni4android; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include "j4a/j4a_base.h"
22+
#include "j4a/j4a_allclasses.include.h"
23+
24+
int J4A_LoadAll__catchAll(JNIEnv *env)
25+
{
26+
int ret = 0;
27+
28+
#include "j4a/j4a_allclasses.loader.h"
29+
30+
fail:
31+
return ret;
32+
}

src/ast__context.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Context
4141

4242
AST_PROPERTY_DEFINE(std::string, h_file_path);
4343
AST_PROPERTY_DEFINE(std::string, c_file_path);
44+
AST_PROPERTY_DEFINE(std::string, j4a_include_file_path);
45+
AST_PROPERTY_DEFINE(std::string, j4a_loader_file_path);
4446
AST_PROPERTY_DEFINE(std::string, java_class_dir);
4547
AST_PROPERTY_DEFINE(Namespace*, global_name_space);
4648

src/ast_class.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,16 @@ class Class: public Member
9090
// @Override
9191
virtual Class *get_this_class() override {return this;}
9292

93-
j4a::string get_c_class_name() {
93+
j4a::string get_c_class_name(bool with_prefix = true) {
9494
std::ostringstream os;
9595
if (get_parent()->get_this_class()) {
9696
// inner class
9797
os << get_parent()->get_this_class()->get_c_class_name();
9898
os << "__";
9999
} else {
100-
os << "J4AC_";
100+
if (with_prefix) {
101+
os << "J4AC_";
102+
}
101103
os << get_this_package()->get_c_long_name();
102104
os << "_";
103105
}

src/ast_compilation_unit.cpp

+35-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ static const char *J4A_LICENSE_HEADER =
5050
" * This file is automatically generated by jni4android, do not modify.\n"
5151
" */\n";
5252

53-
void CompilationUnit::do_build(std::ostream &h_os, std::ostream &c_os)
53+
void CompilationUnit::do_build(
54+
std::ostream &h_os,
55+
std::ostream &c_os,
56+
std::ostream &j4a_include_stream,
57+
std::ostream &j4a_loader_stream)
5458
{
55-
Class *clazz = get_clazz();
59+
Class *clazz = get_clazz();
5660

5761
printf("==========\n");
5862
// printf("%s\n", __func__);
@@ -81,14 +85,24 @@ void CompilationUnit::do_build(std::ostream &h_os, std::ostream &c_os)
8185

8286
clazz->build_c_class_decl(c_os);
8387
clazz->build_c_func_impl(c_os);
88+
89+
// .include.j4a
90+
j4a_include_stream << "#include \"" << get_include_path() << "\"";
91+
92+
// .loader.j4a
93+
j4a_loader_stream << " J4A_LOAD_CLASS(" << clazz->get_c_class_name(false) << ");";
8494
}
8595

8696
void CompilationUnit::build()
8797
{
8898
std::ofstream h_stream;
8999
std::ofstream c_stream;
100+
std::ofstream j4a_include_stream;
101+
std::ofstream j4a_loader_stream;
90102
std::ostream *h_stream_ptr = &std::cout;
91103
std::ostream *c_stream_ptr = &std::cout;
104+
std::ostream *j4a_include_stream_ptr = &std::cout;
105+
std::ostream *j4a_loader_stream_ptr = &std::cout;
92106

93107
if (!Context::instance()->get_h_file_path().empty()) {
94108
h_stream.open(Context::instance()->get_h_file_path().c_str());
@@ -108,5 +122,23 @@ void CompilationUnit::build()
108122
c_stream_ptr = &c_stream;
109123
}
110124

111-
do_build(*h_stream_ptr, *c_stream_ptr);
125+
if (!Context::instance()->get_j4a_include_file_path().empty()) {
126+
j4a_include_stream.open(Context::instance()->get_j4a_include_file_path().c_str());
127+
if (!j4a_include_stream) {
128+
printf("failed to open output .include.j4a file %s\n", Context::instance()->get_j4a_include_file_path().c_str());
129+
return;
130+
}
131+
j4a_include_stream_ptr = &j4a_include_stream;
132+
}
133+
134+
if (!Context::instance()->get_j4a_loader_file_path().empty()) {
135+
j4a_loader_stream.open(Context::instance()->get_j4a_loader_file_path().c_str());
136+
if (!j4a_loader_stream) {
137+
printf("failed to open output .loader.j4a file %s\n", Context::instance()->get_j4a_loader_file_path().c_str());
138+
return;
139+
}
140+
j4a_loader_stream_ptr = &j4a_loader_stream;
141+
}
142+
143+
do_build(*h_stream_ptr, *c_stream_ptr, *j4a_include_stream_ptr, *j4a_loader_stream_ptr);
112144
}

src/ast_compilation_unit.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CompilationUnit: public Node
6262
return os;
6363
}
6464

65-
void do_build(std::ostream &h_os, std::ostream &c_os);
65+
void do_build(std::ostream&, std::ostream&, std::ostream&, std::ostream&);
6666
void build();
6767

6868
public:

src/main.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,38 @@ inline std::string get_h_file(const std::string& value)
161161
return value + ".h";
162162
}
163163

164+
inline std::string get_j4a_include_file(const std::string& value)
165+
{
166+
if (ends_with(value, ".c")) {
167+
std::string result(value, 0, value.length() - 2);
168+
return result + ".include.j4a";
169+
} else if (ends_with(value, ".cpp")) {
170+
std::string result(value, 0, value.length() - 4);
171+
return result + ".include.j4a";
172+
} else if (ends_with(value, ".java")) {
173+
std::string result(value, 0, value.length() - 5);
174+
return result + ".include.j4a";
175+
}
176+
177+
return value + ".include.j4a";
178+
}
179+
180+
inline std::string get_j4a_loader_file(const std::string& value)
181+
{
182+
if (ends_with(value, ".c")) {
183+
std::string result(value, 0, value.length() - 2);
184+
return result + ".loader.j4a";
185+
} else if (ends_with(value, ".cpp")) {
186+
std::string result(value, 0, value.length() - 4);
187+
return result + ".loader.j4a";
188+
} else if (ends_with(value, ".java")) {
189+
std::string result(value, 0, value.length() - 5);
190+
return result + ".loader.j4a";
191+
}
192+
193+
return value + ".loader.j4a";
194+
}
195+
164196
int compile(const std::string& input_file)
165197
{
166198
int ret = 0;
@@ -179,6 +211,8 @@ int compile(const std::string& input_file)
179211
return -1;
180212
}
181213

214+
ast::Context::instance()->set_j4a_include_file_path(get_j4a_include_file(c_file));
215+
ast::Context::instance()->set_j4a_loader_file_path(get_j4a_loader_file(c_file));
182216
ast::Context::instance()->set_h_file_path(h_file.c_str());
183217
ast::Context::instance()->set_c_file_path(c_file.c_str());
184218

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/android/media/AudioTrack.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(android_media_AudioTrack);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/android/media/MediaCodec.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(android_media_MediaCodec);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/android/media/MediaFormat.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(android_media_MediaFormat);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/android/media/PlaybackParams.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(android_media_PlaybackParams);
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/android/os/Build.h"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(android_os_Build);
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/android/os/Bundle.h"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(android_os_Bundle);
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/java/nio/Buffer.h"

test/ref_c/java/nio/Buffer.loader.j4a

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(java_nio_Buffer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/java/nio/ByteBuffer.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(java_nio_ByteBuffer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/java/util/ArrayList.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(java_util_ArrayList);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/tv/danmaku/ijk/media/player/IjkMediaPlayer.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(tv_danmaku_ijk_media_player_IjkMediaPlayer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "j4a/class/tv/danmaku/ijk/media/player/misc/IMediaDataSource.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
J4A_LOAD_CLASS(tv_danmaku_ijk_media_player_misc_IMediaDataSource);

0 commit comments

Comments
 (0)