From df19c13347fa259b44b71b34556eb224546a6833 Mon Sep 17 00:00:00 2001 From: Hemant Zope <42613258+zhemant@users.noreply.github.com> Date: Thu, 6 Dec 2018 04:39:30 +0100 Subject: [PATCH] [C] Resolve some issues from #1473 (#1497) * change folder structure for external and added list as primitive type * modified gitignore for C generator * change include statement as per directory structure * added projectname variable for using in cmakelists * modified cmakefile:compile files irrespective of testfiles and add support for make install * updated readme with how to compile and how to use * CMakeLists change curl to 7.58.0 * fix indentation * add remove as reserved word --- .gitignore | 4 + .../languages/CLibcurlClientCodegen.java | 28 ++++- .../C-libcurl/CMakeLists.txt.mustache | 103 +++++++++--------- .../resources/C-libcurl/README.md.mustache | 36 ++++-- .../resources/C-libcurl/api-body.mustache | 7 +- .../resources/C-libcurl/api-header.mustache | 6 +- .../resources/C-libcurl/apiClient.c.mustache | 3 +- .../resources/C-libcurl/apiClient.h.mustache | 3 +- .../resources/C-libcurl/apiKey.c.mustache | 2 +- .../C-libcurl/keyValuePair.h.mustache | 5 + .../main/resources/C-libcurl/list.c.mustache | 4 +- .../main/resources/C-libcurl/list.h.mustache | 3 +- .../resources/C-libcurl/model-body.mustache | 7 +- .../resources/C-libcurl/model-header.mustache | 4 +- 14 files changed, 130 insertions(+), 85 deletions(-) diff --git a/.gitignore b/.gitignore index aff6667ce66..edc045c83c8 100644 --- a/.gitignore +++ b/.gitignore @@ -212,3 +212,7 @@ samples/client/petstore/javascript/package-lock.json # elm samples/client/petstore/elm/index.html + +# C +samples/client/petstore/c/build +samples/client/petstore/c/*.so diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java index 2c9bb4aa9d8..4dac2b13a44 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java @@ -18,6 +18,8 @@ package org.openapitools.codegen.languages; import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; import org.openapitools.codegen.utils.ModelUtils; @@ -38,7 +40,10 @@ import java.util.Set; public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConfig { private static final Logger LOGGER = LoggerFactory.getLogger(CLibcurlClientCodegen.class); + public static final String PROJECT_NAME = "projectName"; protected String moduleName; + protected String projectName; + protected static final String defaultProjectName = "openapi_client"; protected String specFolder = "spec"; protected String libFolder = "lib"; protected String apiDocPath = "docs/"; @@ -93,6 +98,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf "int", "long", "register", + "remove", "restrict", "return", "short", @@ -135,6 +141,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf languageSpecificPrimitives.add("FILE"); languageSpecificPrimitives.add("Object"); languageSpecificPrimitives.add("list_t*"); + languageSpecificPrimitives.add("list"); typeMapping.put("string", "char"); typeMapping.put("char", "char"); @@ -200,8 +207,8 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf supportingFiles.add(new SupportingFile("list.h.mustache", "include", "list.h")); // external folder supportingFiles.add(new SupportingFile("cJSON.licence.mustache", "external", "cJSON.licence")); - supportingFiles.add(new SupportingFile("cJSON.c.mustache", "external" + File.separator + "src", "cJSON.c")); - supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external" + File.separator + "include", "cJSON.h")); + supportingFiles.add(new SupportingFile("cJSON.c.mustache", "external", "cJSON.c")); + supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external", "cJSON.h")); } @@ -500,7 +507,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf @Override public String toModelImport(String name) { - return "#include \"" + name + ".h\""; + return "#include \"" +"../model/" + name + ".h\""; } @Override @@ -567,6 +574,21 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf p.example = example; } + @Override + public void preprocessOpenAPI(OpenAPI openAPI) { + if (openAPI.getInfo() != null) { + Info info = openAPI.getInfo(); + setProjectName((escapeText(info.getTitle()))); + } else { + setProjectName(defaultProjectName); + } + additionalProperties.put(PROJECT_NAME, projectName); + } + + public void setProjectName(String projectName) { + this.projectName = underscore(projectName.toLowerCase(Locale.ROOT)); + } + @Override public boolean shouldOverwrite(String filename) { // skip spec file as the file might have been updated with new test cases diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache index 4b760d96a5b..ce82e12f34c 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache @@ -1,20 +1,14 @@ cmake_minimum_required (VERSION 2.6) project (CGenerator) -file(GLOB SRC_C src/*.c) -#file(GLOB UNIT_TESTS_C unit-tests/*.c) -#file(GLOB UNIT_TEST_C unit-test/*.c) -file(GLOB MODEL_C model/*.c) -file(GLOB API_C api/*.c) -file(GLOB EXTERNAL_SRC_C external/src/*.c) -set(ALL_SRC_LIST ${SRC_C} ${UNIT_TESTS_C} ${UNIT_TEST_C} ${MODEL_C} ${API_C}) +cmake_policy(SET CMP0063 NEW) + +set(CMAKE_C_VISIBILITY_PRESET default) +set(CMAKE_CXX_VISIBILITY_PRESET default) +set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_BUILD_TYPE Debug) -include(CTest) -include_directories(include) -include_directories(external/include) -include_directories(model) -include_directories(api) +set(pkgName "{{projectName}}") find_program(VALGRIND valgrind) if(VALGRIND) @@ -23,7 +17,7 @@ if(VALGRIND) set(VALGRIND_LIST "") endif() -find_package(CURL 7.61.1 REQUIRED) +find_package(CURL 7.58.0 REQUIRED) if(CURL_FOUND) include_directories(${CURL_INCLUDE_DIR}) set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) @@ -31,46 +25,49 @@ else(CURL_FOUND) message(FATAL_ERROR "Could not find the CURL library and development files.") endif() -# comment out below as auto-generated test file is not supported at the moment -#foreach(ELEMENT ${UNIT_TESTS_C}) -# get_filename_component(ELEMENT_NAME ${ELEMENT} NAME_WE) -# string(REGEX REPLACE "\\.c$" "" ELEMENT_REPLACED ${ELEMENT_NAME}) -# set(EXE_NAME unit-${ELEMENT_REPLACED}) -# add_executable(${EXE_NAME} ${ELEMENT} ${SRC_C} ${MODEL_C} ${API_C} ${EXTERNAL_SRC_C}) -# target_link_libraries(${EXE_NAME} ${CURL_LIBRARIES}) -# add_test(NAME ${EXE_NAME} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}) -# -# if(VALGRIND) -# set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}") -# separate_arguments(memcheck_command) -# -# add_test( -# NAME valgrind-test-${ELEMENT_REPLACED} -# COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME} -# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} -# ) -# endif() -#endforeach() -# -#foreach(ELEMENT ${UNIT_TEST_C}) -#get_filename_component(ELEMENT_NAME ${ELEMENT} NAME_WE) -#string(REGEX REPLACE "\\.c$" "" ELEMENT_REPLACED ${ELEMENT_NAME}) -#set(EXE_NAME unit-${ELEMENT_REPLACED}) -#add_executable(${EXE_NAME} ${ELEMENT} ${SRC_C} ${MODEL_C} ${API_C} ${EXTERNAL_SRC_C}) -#target_link_libraries(${EXE_NAME} ${CURL_LIBRARIES}) -#add_test(NAME ${EXE_NAME} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}) -# -#if(VALGRIND) -#set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}") -#separate_arguments(memcheck_command) -# -#add_test( -#NAME valgrind-test-${ELEMENT_REPLACED} -#COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME} -#WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} -#) -#endif() -#endforeach() +set(SRCS + src/list.c + src/apiKey.c + src/apiClient.c + external/cJSON.c +{{#models}} +{{#model}} + model/{{classname}}.c +{{/model}} +{{/models}} +{{#apiInfo}} +{{#apis}} +{{#operations}} + api/{{classname}}.c +{{/operations}} +{{/apis}} +{{/apiInfo}} + +) + +set(HDRS + include/apiClient.h + include/list.h + include/keyValuePair.h + external/cJSON.h +{{#models}} +{{#model}} + model/{{classname}}.h +{{/model}} +{{/models}} +{{#apiInfo}} +{{#apis}} +{{#operations}} + api/{{classname}}.h +{{/operations}} +{{/apis}} +{{/apiInfo}} + +) + +add_library(${pkgName} SHARED ${SRCS} ${HDRS}) +target_link_libraries(${pkgName} ${CURL_LIBRARIES} ) +install(TARGETS ${pkgName} DESTINATION ${CMAKE_INSTALL_PREFIX}) #For common coding standard (code beautifier/pretty printing) find_program(UNCRUSTIFY uncrustify) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache index 74780db2f6b..c0fdb1883fb 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache @@ -14,20 +14,21 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) {{/infoUrl}} ## Installation -You'll need the `curl 7.61.1` package in order to build the API. To have code formatted nicely you also need to have uncrustify version 0.67. +You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely you also need to have uncrustify version 0.67. -### Prerequisites -Install the `curl 7.61.1` package with the following command on Linux. +# Prerequisites + +## Install the `curl 7.58.0` package with the following command on Linux. ```bash sudo apt remove curl -wget http://curl.haxx.se/download/curl-7.61.1.tar.gz -tar -xvf curl-7.61.1.tar.gz -cd curl-7.61.1/ +wget http://curl.haxx.se/download/curl-7.58.0.tar.gz +tar -xvf curl-7.58.0.tar.gz +cd curl-7.58.0/ ./configure make sudo make install ``` -Install the `uncrustify 0.67` package with the following command on Linux. +## Install the `uncrustify 0.67` package with the following command on Linux. ```bash git clone https://github.com/uncrustify/uncrustify.git cd uncrustify @@ -38,6 +39,27 @@ make sudo make install ``` +## Compile the sample: +```bash +mkdir build +cd build +// To install library to specific location use following command +cmake -DCMAKE_INSTALL_PREFIX=/pathtolocaiton .. +// for normal install use following command +cmake .. +make +sudo make install +``` +## How to use compiled library +Considering the test/source code which uses the API is written in main.c(respective api include is written and all objects necessary are defined) + +To compile main.c use following command +-L - locaiton of the library(not required if cmake with normal installation is performed) +-l library name +```bash +gcc main.c -L. -lpetstore -o main +``` + ## Author diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache index a2c91e9116c..77d26a0280b 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache @@ -1,11 +1,8 @@ #include #include #include -#include "apiClient.h" -#include "cJSON.h" -#include "keyValuePair.h" -{{#imports}}{{{import}}} -{{/imports}} +#include "{{classname}}.h" + #define MAX_BUFFER_LENGTH 4096 #define intToStr(dst, src) \ diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache index 37b49a4d352..7ae0077fb9c 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache @@ -1,7 +1,9 @@ #include #include -#include "apiClient.h" -#include "cJSON.h" +#include "../include/apiClient.h" +#include "../include/list.h" +#include "../external/cJSON.h" +#include "../include/keyValuePair.h" {{#imports}}{{{import}}} {{/imports}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index df398449227..1d591214c45 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -2,8 +2,7 @@ #include #include #include -#include "apiClient.h" -#include "keyValuePair.h" +#include "../include/apiClient.h" size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache index 1f5e9f47b66..721c80dd072 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache @@ -1,7 +1,8 @@ #ifndef INCLUDE_API_CLIENT_H #define INCLUDE_API_CLIENT_H -#include "list.h" +#include "../include/list.h" +#include "../include/keyValuePair.h" typedef int bool; #define true 1 diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache index 9e68c0b21bd..2bf8fc3e9d0 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache @@ -1,6 +1,6 @@ #include #include -#include "keyValuePair.h" +#include "../include/keyValuePair.h" keyValuePair_t *keyValuePair_create(char *key, void *value) { keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t)); diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache index c022337904c..90f92e71f66 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache @@ -1,3 +1,6 @@ +#ifndef _keyValuePair_H_ +#define _keyValuePair_H_ + #include typedef struct keyValuePair_t { @@ -8,3 +11,5 @@ typedef struct keyValuePair_t { keyValuePair_t *keyValuePair_create(char *key, void *value); void keyValuePair_free(keyValuePair_t *keyValuePair); + +#endif /* _keyValuePair_H_ */ \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache index d6fec77d0c3..73eee13c24d 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache @@ -2,9 +2,7 @@ #include #include -#include "cJSON.h" -#include "list.h" - +#include "../include/list.h" static listEntry_t *listEntry_create(void *data) { listEntry_t *createdListEntry = malloc(sizeof(listEntry_t)); if(createdListEntry == NULL) { diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache index 8540178c692..7d98d7f306b 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache @@ -1,7 +1,8 @@ #ifndef INCLUDE_LIST_H #define INCLUDE_LIST_H -#include "cJSON.h" +#include "../external/cJSON.h" +#include "../include/list.h" typedef struct list_t list_t; diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache index 6dbe4a9fd55..84229934148 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache @@ -3,13 +3,8 @@ #include #include #include -#include "cJSON.h" -#include "list.h" -#include "keyValuePair.h" #include "{{classname}}.h" -{{#imports}} -#include "{{{.}}}.h" -{{/imports}} + {{#vars}} {{#isEnum}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache index 84ebe75f826..0ade403852a 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache @@ -8,7 +8,9 @@ #define _{{classname}}_H_ #include -#include "cJSON.h" +#include "../external/cJSON.h" +#include "../include/list.h" +#include "../include/keyValuePair.h" {{#imports}} #include "{{{.}}}.h" {{/imports}}