Skip to content

Commit 46e6b19

Browse files
committed
SocketOptionRegistry.java: Use real C program instead of CPP only to evaluate header constants
1 parent 5da87e0 commit 46e6b19

File tree

3 files changed

+146
-164
lines changed

3 files changed

+146
-164
lines changed

make/modules/java.base/gensrc/GensrcMisc.gmk

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,6 @@ $(eval $(call SetupTextFileProcessing, BUILD_PLATFORMPROPERTIES_JAVA, \
8282
TARGETS += $(BUILD_VERSION_JAVA) $(BUILD_PLATFORMPROPERTIES_JAVA)
8383
################################################################################
8484

85-
ifneq ($(filter $(TOOLCHAIN_TYPE), gcc clang), )
86-
# Need to specify language since the template file has a non standard
87-
# extension.
88-
CPP_FLAGS += -x c
89-
else ifeq ($(TOOLCHAIN_TYPE), microsoft)
90-
CPP_FLAGS += -nologo
91-
92-
ifeq ($(OPENJDK_TARGET_CPU),aarch64)
93-
# cl.exe does only recognize few file extensions as valid (ex: .c, .h, .cpp), so
94-
# make sure *.java.template files are recognized as valid input files
95-
CPP_FILEPREFIX = -Tc
96-
endif
97-
endif
98-
9985
# Generate a java source file from a template through the C preprocessor for the
10086
# target system. First extract the copyright notice at the start of the file.
10187
# Run the preprocessor. Filter out the default compiler stderr output on
@@ -107,7 +93,7 @@ define generate-preproc-src
10793
$(call MakeDir, $(@D))
10894
$(call ExecuteWithLog, $(SUPPORT_OUTPUTDIR)/gensrc/java.base/_$(@F), \
10995
( $(AWK) '/@@END_COPYRIGHT@@/{exit}1' $< && \
110-
$(CPP) $(CPP_FLAGS) $(SYSROOT_CFLAGS) $(CFLAGS_JDKLIB) $(CPP_FILEPREFIX) $< \
96+
($(CC) $(SYSROOT_CFLAGS) $(CFLAGS_JDKLIB) $< -o [email protected] && [email protected] ) \
11197
2> >($(GREP) -v '^$(<F)$$' >&2) \
11298
| $(AWK) '/@@START_HERE@@/,0' \
11399
| $(SED) -e 's/@@START_HERE@@/\/\/ AUTOMATICALLY GENERATED FILE - DO NOT EDIT/' \
@@ -119,7 +105,7 @@ endef
119105
GENSRC_SOR_FILE += $(SUPPORT_OUTPUTDIR)/gensrc/java.base/sun/nio/ch/SocketOptionRegistry.java
120106

121107
$(GENSRC_SOR_FILE): \
122-
$(TOPDIR)/src/java.base/share/classes/sun/nio/ch/SocketOptionRegistry.java.template
108+
$(TOPDIR)/src/java.base/share/classes/sun/nio/ch/SocketOptionRegistry.template.c
123109
$(generate-preproc-src)
124110

125111
TARGETS += $(GENSRC_SOR_FILE)

src/java.base/share/classes/sun/nio/ch/SocketOptionRegistry.java.template

Lines changed: 0 additions & 148 deletions
This file was deleted.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*
26+
*/
27+
// @@END_COPYRIGHT@@
28+
29+
#include <stdio.h>
30+
#ifdef _WIN32
31+
#include <winsock2.h>
32+
#include <ws2tcpip.h>
33+
#else
34+
#include <sys/types.h>
35+
#include <sys/socket.h>
36+
#include <netinet/in.h>
37+
#include <netinet/tcp.h>
38+
#endif
39+
40+
/* Defines SO_REUSEPORT */
41+
#if !defined(SO_REUSEPORT)
42+
#ifdef _WIN32
43+
#define SO_REUSEPORT 0
44+
#elif defined(__linux__)
45+
#define SO_REUSEPORT 15
46+
#elif defined(AIX) || defined(MACOSX)
47+
#define SO_REUSEPORT 0x0200
48+
#else
49+
#define SO_REUSEPORT 0
50+
#endif
51+
#endif
52+
53+
/* To be able to name the Java constants the same as the C constants without
54+
having the preprocessor rewrite those identifiers, add PREFIX_ to all
55+
identifiers matching a C constant. The PREFIX_ is filtered out in the
56+
makefile. */
57+
58+
// @@START_HERE@@
59+
60+
const char *pre =
61+
"package sun.nio.ch;\n"
62+
"import java.net.SocketOption;\n"
63+
"import java.net.StandardSocketOptions;\n"
64+
"import java.net.ProtocolFamily;\n"
65+
"import java.net.StandardProtocolFamily;\n"
66+
"import java.util.Map;\n"
67+
"import java.util.HashMap;\n"
68+
"class SocketOptionRegistry {\n"
69+
"\n"
70+
" private SocketOptionRegistry() { }\n"
71+
"\n"
72+
" private static class RegistryKey {\n"
73+
" private final SocketOption<?> name;\n"
74+
" private final ProtocolFamily family;\n"
75+
" RegistryKey(SocketOption<?> name, ProtocolFamily family) {\n"
76+
" this.name = name;\n"
77+
" this.family = family;\n"
78+
" }\n"
79+
" public int hashCode() {\n"
80+
" return name.hashCode() + family.hashCode();\n"
81+
" }\n"
82+
" public boolean equals(Object ob) {\n"
83+
" if (ob == null) return false;\n"
84+
" if (!(ob instanceof RegistryKey)) return false;\n"
85+
" RegistryKey other = (RegistryKey)ob;\n"
86+
" if (this.name != other.name) return false;\n"
87+
" if (this.family != other.family) return false;\n"
88+
" return true;\n"
89+
" }\n"
90+
" }\n"
91+
"\n"
92+
" private static class LazyInitialization {\n"
93+
"\n"
94+
" static final Map<RegistryKey,OptionKey> options = options();\n"
95+
"\n"
96+
" private static Map<RegistryKey,OptionKey> options() {\n"
97+
" Map<RegistryKey,OptionKey> map =\n"
98+
" new HashMap<RegistryKey,OptionKey>();\n";
99+
100+
const char *post =
101+
"\n"
102+
" return map;\n"
103+
" }\n"
104+
" }\n"
105+
"\n"
106+
" public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) {\n"
107+
" RegistryKey key = new RegistryKey(name, family);\n"
108+
" return LazyInitialization.options.get(key);\n"
109+
" }\n"
110+
"}\n";
111+
112+
int main(void) {
113+
printf("%s\n", pre);
114+
115+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_BROADCAST, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_BROADCAST);
116+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_KEEPALIVE, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_KEEPALIVE);
117+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_LINGER, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_LINGER);
118+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_SNDBUF, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_SNDBUF);
119+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_RCVBUF, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_RCVBUF);
120+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEADDR, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_REUSEADDR);
121+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEPORT, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_REUSEPORT);
122+
123+
printf("// IPPROTO_TCP is 6\n");
124+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_TCP_NODELAY, Net.UNSPEC), new OptionKey(6, %d));\n", TCP_NODELAY);
125+
126+
printf("// IPPROTO_IP is 0\n");
127+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_TOS);
128+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_MULTICAST_IF);
129+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_TTL, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_MULTICAST_TTL);
130+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_LOOP, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_MULTICAST_LOOP);
131+
132+
#ifdef AF_INET6
133+
printf("// IPPROTO_IPV6 is 41\n");
134+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_TCLASS);
135+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_MULTICAST_IF);
136+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_TTL, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_MULTICAST_HOPS);
137+
printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_LOOP, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_MULTICAST_LOOP);
138+
#endif
139+
140+
printf("map.put(new RegistryKey(ExtendedSocketOption.PREFIX_SO_OOBINLINE, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_OOBINLINE);
141+
142+
printf("%s\n", post);
143+
return 0;
144+
}

0 commit comments

Comments
 (0)