Add a Python target for Charmonizer.
diff --git a/buildbin/meld.pl b/buildbin/meld.pl
index a615331..4f84721 100755
--- a/buildbin/meld.pl
+++ b/buildbin/meld.pl
@@ -73,6 +73,7 @@
ConfWriter
ConfWriterC
ConfWriterPerl
+ ConfWriterPython
ConfWriterRuby
HeaderChecker
Make
diff --git a/src/Charmonizer/Core/ConfWriterPython.c b/src/Charmonizer/Core/ConfWriterPython.c
new file mode 100644
index 0000000..e4cc58b
--- /dev/null
+++ b/src/Charmonizer/Core/ConfWriterPython.c
@@ -0,0 +1,223 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Charmonizer/Core/Util.h"
+#include "Charmonizer/Core/ConfWriter.h"
+#include "Charmonizer/Core/ConfWriterPython.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Static vars. */
+static struct {
+ FILE *fh;
+} chaz_CWPython = { NULL };
+static chaz_ConfWriter CWPython_conf_writer;
+
+/* Open the charmony.py file handle.
+ */
+static void
+chaz_ConfWriterPython_open_config_py(void);
+
+static void
+chaz_ConfWriterPython_clean_up(void);
+static void
+chaz_ConfWriterPython_vappend_conf(const char *fmt, va_list args);
+static void
+chaz_ConfWriterPython_add_def(const char *sym, const char *value);
+static void
+chaz_ConfWriterPython_add_global_def(const char *sym, const char *value);
+static void
+chaz_ConfWriterPython_add_typedef(const char *type, const char *alias);
+static void
+chaz_ConfWriterPython_add_global_typedef(const char *type, const char *alias);
+static void
+chaz_ConfWriterPython_add_sys_include(const char *header);
+static void
+chaz_ConfWriterPython_add_local_include(const char *header);
+static void
+chaz_ConfWriterPython_start_module(const char *module_name);
+static void
+chaz_ConfWriterPython_end_module(void);
+
+void
+chaz_ConfWriterPython_enable(void) {
+ CWPython_conf_writer.clean_up = chaz_ConfWriterPython_clean_up;
+ CWPython_conf_writer.vappend_conf = chaz_ConfWriterPython_vappend_conf;
+ CWPython_conf_writer.add_def = chaz_ConfWriterPython_add_def;
+ CWPython_conf_writer.add_global_def = chaz_ConfWriterPython_add_global_def;
+ CWPython_conf_writer.add_typedef = chaz_ConfWriterPython_add_typedef;
+ CWPython_conf_writer.add_global_typedef = chaz_ConfWriterPython_add_global_typedef;
+ CWPython_conf_writer.add_sys_include = chaz_ConfWriterPython_add_sys_include;
+ CWPython_conf_writer.add_local_include = chaz_ConfWriterPython_add_local_include;
+ CWPython_conf_writer.start_module = chaz_ConfWriterPython_start_module;
+ CWPython_conf_writer.end_module = chaz_ConfWriterPython_end_module;
+ chaz_ConfWriterPython_open_config_py();
+ chaz_ConfWriter_add_writer(&CWPython_conf_writer);
+ return;
+}
+
+static void
+chaz_ConfWriterPython_open_config_py(void) {
+ /* Open the filehandle. */
+ chaz_CWPython.fh = fopen("charmony.py", "w+");
+ if (chaz_CWPython.fh == NULL) {
+ chaz_Util_die("Can't open 'charmony.py': %s", strerror(errno));
+ }
+
+ /* Start the module. */
+ fprintf(chaz_CWPython.fh,
+ "# Auto-generated by Charmonizer. \n"
+ "# DO NOT EDIT THIS FILE!!\n"
+ "\n"
+ "class Charmony(object):\n"
+ " @classmethod\n"
+ " def config(cls):\n"
+ " return cls.defs\n"
+ "\n"
+ " defs = {}\n"
+ "\n"
+ );
+}
+
+static void
+chaz_ConfWriterPython_clean_up(void) {
+ /* No more code necessary to finish charmony.py, so just close. */
+ if (fclose(chaz_CWPython.fh)) {
+ chaz_Util_die("Couldn't close 'charmony.py': %s", strerror(errno));
+ }
+}
+
+static void
+chaz_ConfWriterPython_vappend_conf(const char *fmt, va_list args) {
+ (void)fmt;
+ (void)args;
+}
+
+static char*
+chaz_ConfWriterPython_quotify(const char *string, char *buf, size_t buf_size) {
+ char *quoted = buf;
+
+ /* Don't bother with NULL values here. */
+ if (!string) {
+ return NULL;
+ }
+
+ /* Allocate memory if necessary. */
+ {
+ const char *ptr;
+ size_t space = 3; /* Quotes plus NUL termination. */
+ for (ptr = string; *ptr; ptr++) {
+ if (*ptr == '\'' || *ptr == '\\') {
+ space += 2;
+ }
+ else {
+ space += 1;
+ }
+ }
+ if (space > buf_size) {
+ quoted = (char*)malloc(space);
+ }
+ }
+
+ /* Perform copying and escaping */
+ {
+ const char *ptr;
+ size_t pos = 0;
+ quoted[pos++] = '\'';
+ for (ptr = string; *ptr; ptr++) {
+ if (*ptr == '\'' || *ptr == '\\') {
+ quoted[pos++] = '\\';
+ }
+ quoted[pos++] = *ptr;
+ }
+ quoted[pos++] = '\'';
+ quoted[pos++] = '\0';
+ }
+
+ return quoted;
+}
+
+#define CFPYTHON_MAX_BUF 100
+static void
+chaz_ConfWriterPython_add_def(const char *sym, const char *value) {
+ char sym_buf[CFPYTHON_MAX_BUF + 1];
+ char value_buf[CFPYTHON_MAX_BUF + 1];
+ char *quoted_sym;
+ char *quoted_value;
+
+ /* Quote key. */
+ if (!sym) {
+ chaz_Util_die("Can't handle NULL key");
+ }
+ quoted_sym = chaz_ConfWriterPython_quotify(sym, sym_buf, CFPYTHON_MAX_BUF);
+
+ /* Quote value or use "None". */
+ if (!value) {
+ strcpy(value_buf, "None");
+ quoted_value = value_buf;
+ }
+ else {
+ quoted_value = chaz_ConfWriterPython_quotify(value, value_buf,
+ CFPYTHON_MAX_BUF);
+ }
+
+ fprintf(chaz_CWPython.fh, " defs[%s] = %s\n", quoted_sym, quoted_value);
+
+ if (quoted_sym != sym_buf) { free(quoted_sym); }
+ if (quoted_value != value_buf) { free(quoted_value); }
+}
+
+static void
+chaz_ConfWriterPython_add_global_def(const char *sym, const char *value) {
+ (void)sym;
+ (void)value;
+}
+
+static void
+chaz_ConfWriterPython_add_typedef(const char *type, const char *alias) {
+ (void)type;
+ (void)alias;
+}
+
+static void
+chaz_ConfWriterPython_add_global_typedef(const char *type, const char *alias) {
+ (void)type;
+ (void)alias;
+}
+
+static void
+chaz_ConfWriterPython_add_sys_include(const char *header) {
+ (void)header;
+}
+
+static void
+chaz_ConfWriterPython_add_local_include(const char *header) {
+ (void)header;
+}
+
+static void
+chaz_ConfWriterPython_start_module(const char *module_name) {
+ fprintf(chaz_CWPython.fh, " # %s\n", module_name);
+}
+
+static void
+chaz_ConfWriterPython_end_module(void) {
+ fprintf(chaz_CWPython.fh, "\n");
+}
+
diff --git a/src/Charmonizer/Core/ConfWriterPython.h b/src/Charmonizer/Core/ConfWriterPython.h
new file mode 100644
index 0000000..00f13bf
--- /dev/null
+++ b/src/Charmonizer/Core/ConfWriterPython.h
@@ -0,0 +1,37 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Charmonizer/Core/ConfWriterPython.h -- Write to a Python module file.
+ */
+
+#ifndef H_CHAZ_CONFWRITERPYTHON
+#define H_CHAZ_CONFWRITERPYTHON 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Enable writing config to a Python module file.
+ */
+void
+chaz_ConfWriterPython_enable(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CHAZ_CONFWRITERPYTHON */
+
diff --git a/src/Charmonizer/Probe.c b/src/Charmonizer/Probe.c
index 886589b..68b972b 100644
--- a/src/Charmonizer/Probe.c
+++ b/src/Charmonizer/Probe.c
@@ -22,6 +22,7 @@
#include "Charmonizer/Core/ConfWriter.h"
#include "Charmonizer/Core/ConfWriterC.h"
#include "Charmonizer/Core/ConfWriterPerl.h"
+#include "Charmonizer/Core/ConfWriterPython.h"
#include "Charmonizer/Core/ConfWriterRuby.h"
#include "Charmonizer/Core/Util.h"
#include "Charmonizer/Core/Compiler.h"
@@ -52,6 +53,10 @@
args->charmony_pm = 1;
output_enabled = 1;
}
+ else if (strcmp(arg, "--enable-python") == 0) {
+ args->charmony_py = 1;
+ output_enabled = 1;
+ }
else if (strcmp(arg, "--enable-ruby") == 0) {
args->charmony_rb = 1;
output_enabled = 1;
@@ -103,7 +108,7 @@
chaz_Probe_die_usage(void) {
fprintf(stderr,
"Usage: ./charmonize --cc=CC_COMMAND [--enable-c] "
- "[--enable-perl] [--enable-ruby] -- CFLAGS\n");
+ "[--enable-perl] [--enable-python] [--enable-ruby] -- CFLAGS\n");
exit(1);
}
@@ -135,6 +140,10 @@
chaz_ConfWriterPerl_enable();
output_enabled = true;
}
+ if (args->charmony_py) {
+ chaz_ConfWriterPython_enable();
+ output_enabled = true;
+ }
if (args->charmony_rb) {
chaz_ConfWriterRuby_enable();
output_enabled = true;
diff --git a/src/Charmonizer/Probe.h b/src/Charmonizer/Probe.h
index 7f1e321..6323503 100644
--- a/src/Charmonizer/Probe.h
+++ b/src/Charmonizer/Probe.h
@@ -32,6 +32,7 @@
char cflags[CHAZ_PROBE_MAX_CFLAGS_LEN + 1];
int charmony_h;
int charmony_pm;
+ int charmony_py;
int charmony_rb;
int verbosity;
int write_makefile;
@@ -44,6 +45,7 @@
* APP_NAME --cc=CC_COMMAND
* [--enable-c]
* [--enable-perl]
+ * [--enable-python]
* [--enable-ruby]
* [-- [CFLAGS]]
*