Skip to content

Commit

Permalink
Add gencode and genconfig
Browse files Browse the repository at this point in the history
These files are used during the build process to generate
source files. They are temporary moved from ep_engine to
memcached to simplify the build process. They might be moved
back to ep_engine at a later time.

Change-Id: Iba1c07d35e84d441335806f384eda760d2690477
Reviewed-on: http://review.couchbase.org/29498
Tested-by: Trond Norbye <trond.norbye@gmail.com>
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
  • Loading branch information
trondn committed Oct 16, 2013
1 parent b151b44 commit 90423fb
Show file tree
Hide file tree
Showing 3 changed files with 609 additions and 1 deletion.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Expand Up @@ -105,6 +105,11 @@ ADD_EXECUTABLE(memcached
daemon/thread.c)

ADD_EXECUTABLE(memcached_testapp programs/testapp.c daemon/cache.c)
ADD_EXECUTABLE(gencode programs/gencode.cc)
SET_TARGET_PROPERTIES(gencode PROPERTIES COMPILE_FLAGS -I${CMAKE_CURRENT_SOURCE_DIR}/../libvbucket/include)

ADD_EXECUTABLE(genconfig programs/genconfig.cc)
SET_TARGET_PROPERTIES(genconfig PROPERTIES COMPILE_FLAGS -I${CMAKE_CURRENT_SOURCE_DIR}/../libvbucket/include)


#
Expand All @@ -125,6 +130,10 @@ TARGET_LINK_LIBRARIES(mcd_util platform)
TARGET_LINK_LIBRARIES(memcached mcd_util cbsasl platform ${TCMALLOC_LIBRARY} ${LIBEVENT_LIBRARIES} ${COUCHBASE_NETWORK_LIBS})
TARGET_LINK_LIBRARIES(memcached_testapp mcd_util platform ${LIBEVENT_LIBRARIES} ${COUCHBASE_NETWORK_LIBS})

TARGET_LINK_LIBRARIES(gencode cJSON platform)
TARGET_LINK_LIBRARIES(genconfig cJSON platform)


IF(WIN32)
SET_TARGET_PROPERTIES(memcached PROPERTIES
LINK_FLAGS "/LIBPATH:${DEPS_LIB_DIR}")
Expand Down Expand Up @@ -169,7 +178,7 @@ SET_TARGET_PROPERTIES(fragment_rw_ops PROPERTIES INSTALL_NAME_DIR ${CMAKE_INSTAL
SET_TARGET_PROPERTIES(stdin_term_handler PROPERTIES INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib/memcached)


INSTALL(TARGETS engine_testapp cbsasladm mcstat memcached
INSTALL(TARGETS engine_testapp cbsasladm mcstat memcached gencode genconfig
RUNTIME DESTINATION bin)

ADD_TEST(memcached-sizes memcached_sizes)
Expand Down
209 changes: 209 additions & 0 deletions programs/gencode.cc
@@ -0,0 +1,209 @@
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2012 Couchbase, Inc
*
* Licensed 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 "config.h"
#include <cerrno>
#include <cstdlib>
#include <string.h>
#include <strings.h>
#include <cerrno>
#include <getopt.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctype.h>

#include "cJSON.h"

using namespace std;

/**
* Print the JSON object quoted
*/
static ostream& operator <<(ostream &out, cJSON *json)
{
char *data = cJSON_PrintUnformatted(json);
int ii = 0;

out << '"';
while (data[ii] != '\0') {
if (data[ii] == '"') {
out << '\\';
}
out << data[ii];
++ii;
}

out << '"';
free(data);
return out;
}

static void usage(void)
{
cerr << "Usage: gencode -j JSON -c cfile -h headerfile -f function"
<< endl
<< "\tThe JSON file will be read to generate the c and h file."
<< endl;
exit(EXIT_FAILURE);
}

static void str_replace_char(char* str, char toReplace, char replaceWith) {
int i;
for (i = 0; *(str + i) != '\0'; i++) {
if (*(str + i) == toReplace) {
*(str + i) = replaceWith;
}
}
}

static void all_caps(char* str) {
int i;
for (i = 0; *(str + i) != '\0'; i++) {
if (*(str + i) >= 'a' && *(str + i) <= 'z') {
*(str + i) -= 32;
}
}
}

int main(int argc, char **argv) {
int cmd;
const char *json = NULL;
const char *hfile = NULL;
const char *cfile = NULL;
const char *function = NULL;

while ((cmd = getopt(argc, argv, "j:c:h:f:")) != -1) {
switch (cmd) {
case 'j' :
json = optarg;
break;
case 'c' :
cfile = optarg;
break;
case 'h':
hfile = optarg;
break;
case 'f':
function = optarg;
break;
default:
usage();
}
}

if (json == NULL || hfile == NULL || cfile == NULL || function == NULL) {
usage();
}

struct stat st;
if (stat(json, &st) == -1) {
cerr << "Failed to look up \"" << json << "\": "
<< strerror(errno) << endl;
exit(EXIT_FAILURE);
}

char *data = new char[st.st_size + 1];
data[st.st_size] = 0;
ifstream input(json);
input.read(data, st.st_size);
input.close();

cJSON *c = cJSON_Parse(data);
if (c == NULL) {
cerr << "Failed to parse JSON.. probably syntax error" << endl;
exit(EXIT_FAILURE);
}

ofstream headerfile(hfile);
char* macro = strdup(hfile);
str_replace_char(macro, '/', '_');
str_replace_char(macro, '-', '_');
str_replace_char(macro, '.', '_');
all_caps(macro);

headerfile
<< "/*" << endl
<< " * Copyright 2012 Couchbase, Inc" << endl
<< " *" << endl
<< " * Licensed under the Apache License, Version 2.0 (the \"License\");" << endl
<< " * you may not use this file except in compliance with the License." << endl
<< " * You may obtain a copy of the License at" << endl
<< " *" << endl
<< " * http://www.apache.org/licenses/LICENSE-2.0" << endl
<< " *" << endl
<< " * Unless required by applicable law or agreed to in writing, software" << endl
<< " * distributed under the License is distributed on an \"AS IS\" BASIS," << endl
<< " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." << endl
<< " * See the License for the specific language governing permissions and" << endl
<< " * limitations under the License." << endl
<< " */" << endl
<< endl
<< "/////////////////////////////////" << endl
<< "// Generated file, do not edit //" << endl
<< "/////////////////////////////////" << endl
<< "#ifndef " << macro << "_" << endl
<< "#define " << macro << "_" << endl
<< endl
<< "#include \"config.h\"" << endl
<< endl
<< "#ifdef __cplusplus" << endl
<< "extern \"C\" {" << endl
<< "#endif" << endl
<< endl
<< "const char *" << function << "(void);" << endl
<< endl
<< "#ifdef __cplusplus" << endl
<< "}" << endl
<< "#endif" << endl
<< "#endif // " << macro << "_" << endl;
headerfile.close();

ofstream sourcefile(cfile);
sourcefile
<< "/*" << endl
<< " * Copyright 2012 Couchbase, Inc" << endl
<< " *" << endl
<< " * Licensed under the Apache License, Version 2.0 (the \"License\");" << endl
<< " * you may not use this file except in compliance with the License." << endl
<< " * You may obtain a copy of the License at" << endl
<< " *" << endl
<< " * http://www.apache.org/licenses/LICENSE-2.0" << endl
<< " *" << endl
<< " * Unless required by applicable law or agreed to in writing, software" << endl
<< " * distributed under the License is distributed on an \"AS IS\" BASIS," << endl
<< " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." << endl
<< " * See the License for the specific language governing permissions and" << endl
<< " * limitations under the License." << endl
<< " */" << endl
<< endl
<< "/////////////////////////////////" << endl
<< "// Generated file, do not edit //" << endl
<< "/////////////////////////////////" << endl
<< "#include \"config.h\"" << endl
<< "#include \"" << hfile << "\"" << endl
<< endl
<< "const char *" << function << "(void)" << endl
<< "{" << endl
<< " return " << c << ";" << endl
<< "}" << endl;

cJSON_Delete(c);

return 0;
}

0 comments on commit 90423fb

Please sign in to comment.