2017-05-30 17:15:28 -07:00
|
|
|
/*
|
Audio HAL: Factorize V2 and V4 like libaudiohal
Use the same technique developed for the simpler libaudiohal to
factorize the audio HAL impl.
Generated with script:
set -euo pipefail
cd hardware/interfaces/audio
# Make 2.0 and 4.0 files mostly identical by removing all 2.0, V4.0...
# and replacing it by CPP_VERSION and FILE_VERSION
# This allow both implementation to be mostly identical, except for code difference
# Also remove IncludeGuard.h as it will be included from compiler flag
# Also update license to 2018
find -name *.cpp -o -name *h |
grep -v -e service.cpp |
xargs sed -Ei \
-e 's/(#.*)V[42]_0_(.*_H_?)$/\1\2/' \
-e 's/(LOG_TAG.*)[24]_0/\1/' \
-e '/#ifndef AUDIO_HAL_VERSION/,/#endif/d' \
-e '/^#.*def.*AUDIO_HAL_VERSION\b/d' \
-e's/V[42]_0/CPP_VERSION/' \
-e 's/(#include ).(android.*)[24].0(.*).$/\1PATH(\2FILE_VERSION\3)/' \
-e 's|(#include) .(.*)[24].0/(default/.*).$|\1 "\2\3"|' \
-e 's/\bAUDIO_HAL_VERSION\b/CPP_VERSION/' \
-e '/IncludeGuard.h/d' \
-e 's/(Copyright .C. 201)./\18/'
# set MAJOR_VERSION instead of having a different macro for each version
# this allow to do preprocessor arithmetic (eg: MAJOR_VERSION >= 4)
for v in 2 4; do
find -ipath '*/'$v'.0/*/Android.bp' |
xargs sed -i -e '/cflags:/,/^$/d' -e '/vndk/{:a;/}/!{N;ba};d}' -e '$i\
cflags: [\
"-DMAJOR_VERSION='$v'",\
"-DMINOR_VERSION=0",\
"-include common/all-versions/VersionMacro.h",\
]'; done
# replace# ifdef VERSION_X by #if MAJOR_VERSION == X
find -path *all-versions/*.h |
xargs sed -Ei -e 's/def.*AUDIO_HAL_VERSION_([24])_0.*/ MAJOR_VERSION == \1/' \
-e 'T;s/ +/ /'
# copy all-versions impl in each version impl
find -path '*/[24].0/*.[hc]*' |
grep -ve all-versions -e test |
xargs -P99 -n4 sed -i -Ee '/include <.*all-versions.default/!b' \
-e 's#.*<#find -path */#' -e 's/>$/|xargs tail -n +16/' -e e
# remove all-versions impl
rm -r {core,effect}/all-versions/
# merge version impl into a single all-version
for dir in core/*.0/vts/functional/*.0/ ;do
dest=$(echo $dir | sed 's#/..0/#/all-versions/#')
mkdir -p $dest
mv -T $dir $dest
done
find -mindepth 3 -path '*/2.0/*' -a \( -name '*cpp' -o -name '*h' \) |
grep -v 'all-versions' |
sed -E 'h;s/2/4/g;H;s/4.0/all-versions/;s/4.0//;H;g;s/\n/ /g;'|
xargs -P99 -L1 sh -c '
set -euo pipefail
mkdir -p $(dirname $2);
diff --old-group-format="#if MAJOR_VERSION == 2
%<#endif
" \
--new-group-format="#if MAJOR_VERSION == 4
%>#endif
" $0 $1 > $2 || true;
rm $0 $1'
# merge the X.0 Android.bp & OWNERS in the all-versions
for dir in common effect core; do
for sub in default vts/functional; do
test -f $dir/2.0/$sub/Android.bp || continue
awk 1 $dir/*.0/$sub/Android.bp >> $dir/all-versions/$sub/Android.bp
# delete licenses except for the first one and add whitelines
sed -i -e '/^}$/{N;/^}\n$/!s/\n/&\n/}' $dir/all-versions/$sub/Android.bp
sed -i -e '1,17b;/^\/\//,/^$/{d}' $dir/all-versions/$sub/Android.bp
done
for sub in default vts; do
test -d $dir/2.0/$sub || continue
test -f $dir/2.0/$sub/OWNERS &&
awk 1 $dir/*.0/$sub/OWNERS | sort -u > $dir/all-versions/$sub/OWNERS ||
true
rm -r $dir/*.0/$sub
done
done
# delete all-versions-impl dependencies
find -name 'Android.bp' | xargs sed -i -e '/all-versions-impl/d'
# cleanup unused files
rm common/all-versions/default/include/common/all-versions/default/HidlUtils*
rm common/all-versions/util/include/common/all-versions/IncludeGuard.h
find -depth -type d -empty -delete
# Clamp consecutive number of empty lines to 2
find -name *.cpp -o -name *h | xargs sed -Ei ':a;/^\n*$/{N;ba};s/\n\n+/\n\n/'
# transform #endif\n#if to #elif
find -name *.cpp -o -name *h | xargs sed -i '/^#endif/{N;s/.*\n#if/#elif/}'
# remove leftover include guard in cpp
find -name *.cpp |xargs sed -Ei '/^#.*_H_?$/d'
# apply clang-format
find -name *cpp -o -name *h |
xargs ../../../prebuilts/clang/host/linux-x86/clang-stable/bin/clang-format --style file -i
# clang format breaks PATH(a/b) to PATH(a / b), remove the space surrounding /
find -name *cpp -o -name *h | xargs sed -i "/#include PATH/s# / #/#g"
Test: compile
Bug: 118203066
Change-Id: I3692a444307afc5f71064fe0b9e6b8af3c9ff1dd
Signed-off-by: Kevin Rocard <krocard@google.com>
2018-11-14 16:22:07 -08:00
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
2017-05-30 17:15:28 -07:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define LOG_TAG "ValidateAudioConfig"
|
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
2017-08-21 18:32:49 -07:00
|
|
|
#include <numeric>
|
|
|
|
|
|
2017-05-30 17:15:28 -07:00
|
|
|
#define LIBXML_SCHEMAS_ENABLED
|
|
|
|
|
#include <libxml/xmlschemastypes.h>
|
|
|
|
|
#define LIBXML_XINCLUDE_ENABLED
|
|
|
|
|
#include <libxml/xinclude.h>
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#include "ValidateXml.h"
|
|
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
namespace hardware {
|
|
|
|
|
namespace audio {
|
2017-06-02 11:44:06 -07:00
|
|
|
namespace common {
|
2017-05-30 17:15:28 -07:00
|
|
|
namespace test {
|
2017-06-02 11:44:06 -07:00
|
|
|
namespace utility {
|
2017-05-30 17:15:28 -07:00
|
|
|
|
|
|
|
|
/** Map libxml2 structures to their corresponding deleters. */
|
|
|
|
|
template <class T>
|
|
|
|
|
constexpr void (*xmlDeleter)(T* t);
|
|
|
|
|
template <>
|
|
|
|
|
constexpr auto xmlDeleter<xmlSchema> = xmlSchemaFree;
|
|
|
|
|
template <>
|
|
|
|
|
constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc;
|
|
|
|
|
template <>
|
|
|
|
|
constexpr auto xmlDeleter<xmlSchemaParserCtxt> = xmlSchemaFreeParserCtxt;
|
|
|
|
|
template <>
|
|
|
|
|
constexpr auto xmlDeleter<xmlSchemaValidCtxt> = xmlSchemaFreeValidCtxt;
|
|
|
|
|
|
|
|
|
|
/** @return a unique_ptr with the correct deleter for the libxml2 object. */
|
|
|
|
|
template <class T>
|
|
|
|
|
constexpr auto make_xmlUnique(T* t) {
|
|
|
|
|
// Wrap deleter in lambda to enable empty base optimization
|
|
|
|
|
auto deleter = [](T* t) { xmlDeleter<T>(t); };
|
|
|
|
|
return std::unique_ptr<T, decltype(deleter)>{t, deleter};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Class that handles libxml2 initialization and cleanup. NOT THREAD SAFE*/
|
|
|
|
|
struct Libxml2Global {
|
|
|
|
|
Libxml2Global() {
|
|
|
|
|
xmlLineNumbersDefault(1); // Better error message
|
|
|
|
|
xmlSetGenericErrorFunc(this, errorCb);
|
|
|
|
|
}
|
|
|
|
|
~Libxml2Global() {
|
|
|
|
|
// TODO: check if all those cleanup are needed
|
|
|
|
|
xmlSetGenericErrorFunc(nullptr, nullptr);
|
|
|
|
|
xmlSchemaCleanupTypes();
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
|
xmlCleanupThreads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string& getErrors() { return errors; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static void errorCb(void* ctxt, const char* msg, ...) {
|
|
|
|
|
auto* self = static_cast<Libxml2Global*>(ctxt);
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, msg);
|
|
|
|
|
|
|
|
|
|
char* formatedMsg;
|
|
|
|
|
if (vasprintf(&formatedMsg, msg, args) >= 0) {
|
|
|
|
|
LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", formatedMsg);
|
|
|
|
|
self->errors += "Error: ";
|
|
|
|
|
self->errors += formatedMsg;
|
|
|
|
|
}
|
|
|
|
|
free(formatedMsg);
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
std::string errors;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
::testing::AssertionResult validateXml(const char* xmlFilePathExpr, const char* xsdFilePathExpr,
|
|
|
|
|
const char* xmlFilePath, const char* xsdFilePath) {
|
|
|
|
|
Libxml2Global libxml2;
|
|
|
|
|
|
|
|
|
|
auto context = [&]() {
|
2017-08-21 18:32:49 -07:00
|
|
|
return std::string() + " While validating: " + xmlFilePathExpr +
|
2017-05-30 17:15:28 -07:00
|
|
|
"\n Which is: " + xmlFilePath + "\nAgainst the schema: " + xsdFilePathExpr +
|
2017-08-21 18:32:49 -07:00
|
|
|
"\n Which is: " + xsdFilePath + "\nLibxml2 errors:\n" + libxml2.getErrors();
|
2017-05-30 17:15:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto schemaParserCtxt = make_xmlUnique(xmlSchemaNewParserCtxt(xsdFilePath));
|
|
|
|
|
auto schema = make_xmlUnique(xmlSchemaParse(schemaParserCtxt.get()));
|
|
|
|
|
if (schema == nullptr) {
|
|
|
|
|
return ::testing::AssertionFailure() << "Failed to parse schema (xsd)\n" << context();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto doc = make_xmlUnique(xmlReadFile(xmlFilePath, nullptr, 0));
|
|
|
|
|
if (doc == nullptr) {
|
|
|
|
|
return ::testing::AssertionFailure() << "Failed to parse xml\n" << context();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-03 00:49:13 +00:00
|
|
|
// Process 'include' directives w/o modifying elements loaded from included files.
|
|
|
|
|
if (xmlXIncludeProcessFlags(doc.get(), XML_PARSE_NOBASEFIX) == -1) {
|
2017-05-30 17:15:28 -07:00
|
|
|
return ::testing::AssertionFailure() << "Failed to resolve xincludes in xml\n" << context();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto schemaCtxt = make_xmlUnique(xmlSchemaNewValidCtxt(schema.get()));
|
|
|
|
|
int ret = xmlSchemaValidateDoc(schemaCtxt.get(), doc.get());
|
|
|
|
|
if (ret > 0) {
|
2017-08-21 18:32:49 -07:00
|
|
|
return ::testing::AssertionFailure() << "XML is not valid according to the xsd\n"
|
2017-05-30 17:15:28 -07:00
|
|
|
<< context();
|
|
|
|
|
}
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return ::testing::AssertionFailure() << "Internal or API error\n" << context();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ::testing::AssertionSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 13:31:47 -07:00
|
|
|
template <bool atLeastOneRequired>
|
2017-08-21 18:32:49 -07:00
|
|
|
::testing::AssertionResult validateXmlMultipleLocations(
|
2020-04-14 14:53:22 -07:00
|
|
|
const char* xmlFileNameExpr, const char* xmlFileLocationsExpr, const char* xsdFilePathExpr,
|
|
|
|
|
const char* xmlFileName, const std::vector<std::string>& xmlFileLocations,
|
|
|
|
|
const char* xsdFilePath) {
|
2017-08-21 18:32:49 -07:00
|
|
|
using namespace std::string_literals;
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> errors;
|
|
|
|
|
std::vector<std::string> foundFiles;
|
|
|
|
|
|
2020-04-14 14:53:22 -07:00
|
|
|
for (const auto& location : xmlFileLocations) {
|
2017-08-21 18:32:49 -07:00
|
|
|
std::string xmlFilePath = location + "/"s + xmlFileName;
|
|
|
|
|
if (access(xmlFilePath.c_str(), F_OK) != 0) {
|
|
|
|
|
// If the file does not exist ignore this location and fallback on the next one
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foundFiles.push_back(" " + xmlFilePath + '\n');
|
|
|
|
|
auto result = validateXml("xmlFilePath", xsdFilePathExpr, xmlFilePath.c_str(), xsdFilePath);
|
|
|
|
|
if (!result) {
|
|
|
|
|
errors.push_back(result.message());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 13:31:47 -07:00
|
|
|
if (atLeastOneRequired && foundFiles.empty()) {
|
2017-08-21 18:32:49 -07:00
|
|
|
errors.push_back("No xml file found in provided locations.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ::testing::AssertionResult(errors.empty())
|
|
|
|
|
<< errors.size() << " error" << (errors.size() == 1 ? " " : "s ")
|
|
|
|
|
<< std::accumulate(begin(errors), end(errors), "occurred during xml validation:\n"s)
|
|
|
|
|
<< " While validating all: " << xmlFileNameExpr
|
|
|
|
|
<< "\n Which is: " << xmlFileName
|
|
|
|
|
<< "\n In the following folders: " << xmlFileLocationsExpr
|
2018-07-19 13:31:47 -07:00
|
|
|
<< "\n Which is: " << ::testing::PrintToString(xmlFileLocations)
|
2018-11-21 13:45:54 -08:00
|
|
|
<< (atLeastOneRequired ? "\nWhere at least one file must be found."
|
|
|
|
|
: "\nWhere no file might exist.");
|
2017-08-21 18:32:49 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-14 14:53:22 -07:00
|
|
|
template ::testing::AssertionResult validateXmlMultipleLocations<true>(
|
|
|
|
|
const char*, const char*, const char*, const char*, const std::vector<std::string>&,
|
|
|
|
|
const char*);
|
|
|
|
|
template ::testing::AssertionResult validateXmlMultipleLocations<false>(
|
|
|
|
|
const char*, const char*, const char*, const char*, const std::vector<std::string>&,
|
|
|
|
|
const char*);
|
2018-07-19 13:31:47 -07:00
|
|
|
|
2017-12-14 18:50:12 -08:00
|
|
|
} // namespace utility
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace common
|
|
|
|
|
} // namespace audio
|
|
|
|
|
} // namespace hardware
|
|
|
|
|
} // namespace android
|