[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
This commit is contained in:
Hemant Zope 2018-12-06 04:39:30 +01:00 committed by William Cheng
parent 32d228c33a
commit df19c13347
14 changed files with 130 additions and 85 deletions

4
.gitignore vendored
View File

@ -212,3 +212,7 @@ samples/client/petstore/javascript/package-lock.json
# elm # elm
samples/client/petstore/elm/index.html samples/client/petstore/elm/index.html
# C
samples/client/petstore/c/build
samples/client/petstore/c/*.so

View File

@ -18,6 +18,8 @@ package org.openapitools.codegen.languages;
import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema; 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.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*; import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.ModelUtils;
@ -38,7 +40,10 @@ import java.util.Set;
public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConfig { public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(CLibcurlClientCodegen.class); private static final Logger LOGGER = LoggerFactory.getLogger(CLibcurlClientCodegen.class);
public static final String PROJECT_NAME = "projectName";
protected String moduleName; protected String moduleName;
protected String projectName;
protected static final String defaultProjectName = "openapi_client";
protected String specFolder = "spec"; protected String specFolder = "spec";
protected String libFolder = "lib"; protected String libFolder = "lib";
protected String apiDocPath = "docs/"; protected String apiDocPath = "docs/";
@ -93,6 +98,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
"int", "int",
"long", "long",
"register", "register",
"remove",
"restrict", "restrict",
"return", "return",
"short", "short",
@ -135,6 +141,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
languageSpecificPrimitives.add("FILE"); languageSpecificPrimitives.add("FILE");
languageSpecificPrimitives.add("Object"); languageSpecificPrimitives.add("Object");
languageSpecificPrimitives.add("list_t*"); languageSpecificPrimitives.add("list_t*");
languageSpecificPrimitives.add("list");
typeMapping.put("string", "char"); typeMapping.put("string", "char");
typeMapping.put("char", "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")); supportingFiles.add(new SupportingFile("list.h.mustache", "include", "list.h"));
// external folder // external folder
supportingFiles.add(new SupportingFile("cJSON.licence.mustache", "external", "cJSON.licence")); 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.c.mustache", "external", "cJSON.c"));
supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external" + File.separator + "include", "cJSON.h")); supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external", "cJSON.h"));
} }
@ -500,7 +507,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
@Override @Override
public String toModelImport(String name) { public String toModelImport(String name) {
return "#include \"" + name + ".h\""; return "#include \"" +"../model/" + name + ".h\"";
} }
@Override @Override
@ -567,6 +574,21 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
p.example = example; 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 @Override
public boolean shouldOverwrite(String filename) { public boolean shouldOverwrite(String filename) {
// skip spec file as the file might have been updated with new test cases // skip spec file as the file might have been updated with new test cases

View File

@ -1,20 +1,14 @@
cmake_minimum_required (VERSION 2.6) cmake_minimum_required (VERSION 2.6)
project (CGenerator) project (CGenerator)
file(GLOB SRC_C src/*.c) cmake_policy(SET CMP0063 NEW)
#file(GLOB UNIT_TESTS_C unit-tests/*.c)
#file(GLOB UNIT_TEST_C unit-test/*.c) set(CMAKE_C_VISIBILITY_PRESET default)
file(GLOB MODEL_C model/*.c) set(CMAKE_CXX_VISIBILITY_PRESET default)
file(GLOB API_C api/*.c) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
file(GLOB EXTERNAL_SRC_C external/src/*.c)
set(ALL_SRC_LIST ${SRC_C} ${UNIT_TESTS_C} ${UNIT_TEST_C} ${MODEL_C} ${API_C})
set(CMAKE_BUILD_TYPE Debug) set(CMAKE_BUILD_TYPE Debug)
include(CTest) set(pkgName "{{projectName}}")
include_directories(include)
include_directories(external/include)
include_directories(model)
include_directories(api)
find_program(VALGRIND valgrind) find_program(VALGRIND valgrind)
if(VALGRIND) if(VALGRIND)
@ -23,7 +17,7 @@ if(VALGRIND)
set(VALGRIND_LIST "") set(VALGRIND_LIST "")
endif() endif()
find_package(CURL 7.61.1 REQUIRED) find_package(CURL 7.58.0 REQUIRED)
if(CURL_FOUND) if(CURL_FOUND)
include_directories(${CURL_INCLUDE_DIR}) include_directories(${CURL_INCLUDE_DIR})
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} ) 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.") message(FATAL_ERROR "Could not find the CURL library and development files.")
endif() endif()
# comment out below as auto-generated test file is not supported at the moment set(SRCS
#foreach(ELEMENT ${UNIT_TESTS_C}) src/list.c
# get_filename_component(ELEMENT_NAME ${ELEMENT} NAME_WE) src/apiKey.c
# string(REGEX REPLACE "\\.c$" "" ELEMENT_REPLACED ${ELEMENT_NAME}) src/apiClient.c
# set(EXE_NAME unit-${ELEMENT_REPLACED}) external/cJSON.c
# add_executable(${EXE_NAME} ${ELEMENT} ${SRC_C} ${MODEL_C} ${API_C} ${EXTERNAL_SRC_C}) {{#models}}
# target_link_libraries(${EXE_NAME} ${CURL_LIBRARIES}) {{#model}}
# add_test(NAME ${EXE_NAME} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}) model/{{classname}}.c
# {{/model}}
# if(VALGRIND) {{/models}}
# set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}") {{#apiInfo}}
# separate_arguments(memcheck_command) {{#apis}}
# {{#operations}}
# add_test( api/{{classname}}.c
# NAME valgrind-test-${ELEMENT_REPLACED} {{/operations}}
# COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME} {{/apis}}
# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} {{/apiInfo}}
# )
# endif() )
#endforeach()
# set(HDRS
#foreach(ELEMENT ${UNIT_TEST_C}) include/apiClient.h
#get_filename_component(ELEMENT_NAME ${ELEMENT} NAME_WE) include/list.h
#string(REGEX REPLACE "\\.c$" "" ELEMENT_REPLACED ${ELEMENT_NAME}) include/keyValuePair.h
#set(EXE_NAME unit-${ELEMENT_REPLACED}) external/cJSON.h
#add_executable(${EXE_NAME} ${ELEMENT} ${SRC_C} ${MODEL_C} ${API_C} ${EXTERNAL_SRC_C}) {{#models}}
#target_link_libraries(${EXE_NAME} ${CURL_LIBRARIES}) {{#model}}
#add_test(NAME ${EXE_NAME} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}) model/{{classname}}.h
# {{/model}}
#if(VALGRIND) {{/models}}
#set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}") {{#apiInfo}}
#separate_arguments(memcheck_command) {{#apis}}
# {{#operations}}
#add_test( api/{{classname}}.h
#NAME valgrind-test-${ELEMENT_REPLACED} {{/operations}}
#COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME} {{/apis}}
#WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} {{/apiInfo}}
#)
#endif() )
#endforeach()
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) #For common coding standard (code beautifier/pretty printing)
find_program(UNCRUSTIFY uncrustify) find_program(UNCRUSTIFY uncrustify)

View File

@ -14,20 +14,21 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
{{/infoUrl}} {{/infoUrl}}
## Installation ## 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 # Prerequisites
Install the `curl 7.61.1` package with the following command on Linux.
## Install the `curl 7.58.0` package with the following command on Linux.
```bash ```bash
sudo apt remove curl sudo apt remove curl
wget http://curl.haxx.se/download/curl-7.61.1.tar.gz wget http://curl.haxx.se/download/curl-7.58.0.tar.gz
tar -xvf curl-7.61.1.tar.gz tar -xvf curl-7.58.0.tar.gz
cd curl-7.61.1/ cd curl-7.58.0/
./configure ./configure
make make
sudo make install 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 ```bash
git clone https://github.com/uncrustify/uncrustify.git git clone https://github.com/uncrustify/uncrustify.git
cd uncrustify cd uncrustify
@ -38,6 +39,27 @@ make
sudo make install 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 ## Author

View File

@ -1,11 +1,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include "apiClient.h" #include "{{classname}}.h"
#include "cJSON.h"
#include "keyValuePair.h"
{{#imports}}{{{import}}}
{{/imports}}
#define MAX_BUFFER_LENGTH 4096 #define MAX_BUFFER_LENGTH 4096
#define intToStr(dst, src) \ #define intToStr(dst, src) \

View File

@ -1,7 +1,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "apiClient.h" #include "../include/apiClient.h"
#include "cJSON.h" #include "../include/list.h"
#include "../external/cJSON.h"
#include "../include/keyValuePair.h"
{{#imports}}{{{import}}} {{#imports}}{{{import}}}
{{/imports}} {{/imports}}

View File

@ -2,8 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "apiClient.h" #include "../include/apiClient.h"
#include "keyValuePair.h"
size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);

View File

@ -1,7 +1,8 @@
#ifndef INCLUDE_API_CLIENT_H #ifndef INCLUDE_API_CLIENT_H
#define INCLUDE_API_CLIENT_H #define INCLUDE_API_CLIENT_H
#include "list.h" #include "../include/list.h"
#include "../include/keyValuePair.h"
typedef int bool; typedef int bool;
#define true 1 #define true 1

View File

@ -1,6 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "keyValuePair.h" #include "../include/keyValuePair.h"
keyValuePair_t *keyValuePair_create(char *key, void *value) { keyValuePair_t *keyValuePair_create(char *key, void *value) {
keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t)); keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t));

View File

@ -1,3 +1,6 @@
#ifndef _keyValuePair_H_
#define _keyValuePair_H_
#include<string.h> #include<string.h>
typedef struct keyValuePair_t { typedef struct keyValuePair_t {
@ -8,3 +11,5 @@ typedef struct keyValuePair_t {
keyValuePair_t *keyValuePair_create(char *key, void *value); keyValuePair_t *keyValuePair_create(char *key, void *value);
void keyValuePair_free(keyValuePair_t *keyValuePair); void keyValuePair_free(keyValuePair_t *keyValuePair);
#endif /* _keyValuePair_H_ */

View File

@ -2,9 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "cJSON.h" #include "../include/list.h"
#include "list.h"
static listEntry_t *listEntry_create(void *data) { static listEntry_t *listEntry_create(void *data) {
listEntry_t *createdListEntry = malloc(sizeof(listEntry_t)); listEntry_t *createdListEntry = malloc(sizeof(listEntry_t));
if(createdListEntry == NULL) { if(createdListEntry == NULL) {

View File

@ -1,7 +1,8 @@
#ifndef INCLUDE_LIST_H #ifndef INCLUDE_LIST_H
#define INCLUDE_LIST_H #define INCLUDE_LIST_H
#include "cJSON.h" #include "../external/cJSON.h"
#include "../include/list.h"
typedef struct list_t list_t; typedef struct list_t list_t;

View File

@ -3,13 +3,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "cJSON.h"
#include "list.h"
#include "keyValuePair.h"
#include "{{classname}}.h" #include "{{classname}}.h"
{{#imports}}
#include "{{{.}}}.h"
{{/imports}}
{{#vars}} {{#vars}}
{{#isEnum}} {{#isEnum}}

View File

@ -8,7 +8,9 @@
#define _{{classname}}_H_ #define _{{classname}}_H_
#include <string.h> #include <string.h>
#include "cJSON.h" #include "../external/cJSON.h"
#include "../include/list.h"
#include "../include/keyValuePair.h"
{{#imports}} {{#imports}}
#include "{{{.}}}.h" #include "{{{.}}}.h"
{{/imports}} {{/imports}}